Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grid Telerik网格和循环参考异常_Grid_Json.net_Telerik Mvc_Circular Reference - Fatal编程技术网

Grid Telerik网格和循环参考异常

Grid Telerik网格和循环参考异常,grid,json.net,telerik-mvc,circular-reference,Grid,Json.net,Telerik Mvc,Circular Reference,当我试图在其绑定中使用entityframework poco类时,Telerik Grid抛出循环引用异常。链接中提到的代码建议将Telerik使用的json序列化程序替换为NewtonSoft。但是Telerik Grid从未从注入网格的CustomGridActionResultFactory调用create方法。有人知道这段代码()的问题吗?有一个演示如何创建自定义GridActionResultFactory的示例。您可能会发现它很有用。您应该为TelerikGrid使用“特殊”视图模

当我试图在其绑定中使用entityframework poco类时,Telerik Grid抛出循环引用异常。链接中提到的代码建议将Telerik使用的json序列化程序替换为NewtonSoft。但是Telerik Grid从未从注入网格的CustomGridActionResultFactory调用create方法。有人知道这段代码()的问题吗?

有一个演示如何创建自定义GridActionResultFactory的示例。您可能会发现它很有用。

您应该为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; }   
}  

如果你喜欢我,认为仅仅为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库:

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

    }