【问题标题】:Telerik Grid and Circular reference exceptionTelerik 网格和循环引用异常
【发布时间】:2011-11-30 15:43:58
【问题描述】:

I would like to try this

当我尝试在其绑定中使用实体框架 poco 类时,Telerik Grid 引发循环引用异常。链接中提到的代码建议用 NewtonSoft 替换 Telerik 使用的 json 序列化程序。但是 Telerik Grid 从不调用从 CustomGridActionResultFactory 注入到 Grid 中的 create 方法。有人知道这段代码的问题吗 (link above)?

【问题讨论】:

    标签: grid json.net telerik-mvc circular-reference


    【解决方案1】:

    有一个code library project,它展示了如何创建一个自定义的 GridActionResultFactory。您可能会发现它很有帮助。

    【讨论】:

      【解决方案2】:

      您应该为 TelerikGrid 使用“特殊”视图模型。

      例如,如果您使用类似这样的数据库模型

      public class Master {  
          public int Id { get; set; }  
          public string Name { get; set; }  
          public List<Detail> Details { get; set; }   
      }  
      
      public class Detail {  
          public int Id { get; set; }  
          public string Name { get; set; }  
          public Master Master { get; set; }   
      }  
      

      您需要创建如下所示的视图模型

      public class MasterView {  
          public int Id { get; set; }  
          public string Name { get; set; }   
      }  
      
      public class DetailView {  
          public int Id { get; set; }  
          public string Name { get; set; }  
          public int MasterId { get; set; }   
      }  
      

      【讨论】:

        【解决方案3】:

        如果你喜欢我,认为为 Telerik 网格创建单独的视图模型是多余的,你可以将自己的 Json 提供给从匿名对象列表创建的网格:

        //This goes in the View:
        Html.Telerik().Grid(Model)
            .Name("Grid")
            .Columns(columns =>
            {
            // your column mappings go here
            })
            .DataBinding(dataBinding => dataBinding.Ajax().Select("_yourMethodReturningJson", "YourControllerName", new { yourJsonMethodParameter = yourViewModel.someField }))
            .Pageable()
            .Sortable()
            .Render();
        

        你控制器中的 Json 方法如下所示:

            public JsonResult _yourMethodReturningJson(YourType? yourJsonMethodParameter)
            {
                var list = database.SomeCollection.Select(x => new
                    {
                        SomeColumnName = x.SomeField
                    });
                return Json(list, JsonRequestBehavior.AllowGet);
        
            }
        

        如果您愿意,可以在这里使用更好的 Json 库:http://james.newtonking.com/pages/json-net.aspx

        【讨论】:

          猜你喜欢
          • 2011-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-05
          • 1970-01-01
          • 1970-01-01
          • 2019-03-10
          • 1970-01-01
          • 2020-08-01
          相关资源
          最近更新 更多