C# 使用EF为trirand.jqgrid网格提供哪些数据源
到目前为止,我有这个,但它不工作。我得到了一个现有的datareader错误,但后来我添加了MARS属性并将其设置为true,我遇到了一个新问题,即它是一个空引用。我不确定如何设置数据源,以便在使用EF的数据库的视图中填充jqgrid。我看到了很多例子,但它们对我的情况没有多大帮助C# 使用EF为trirand.jqgrid网格提供哪些数据源,c#,asp.net-mvc,entity-framework,jqgrid,C#,Asp.net Mvc,Entity Framework,Jqgrid,到目前为止,我有这个,但它不工作。我得到了一个现有的datareader错误,但后来我添加了MARS属性并将其设置为true,我遇到了一个新问题,即它是一个空引用。我不确定如何设置数据源,以便在使用EF的数据库的视图中填充jqgrid。我看到了很多例子,但它们对我的情况没有多大帮助 public JsonResult BugJqGridDataRequested() { var bugGrid = new BugJqGridViewModel(); va
public JsonResult BugJqGridDataRequested()
{
var bugGrid = new BugJqGridViewModel();
var db = new BugContext();
var bugs = db.Bugs.ToList();
bugGrid.Grid.DataSource = bugs;
return bugGrid.Grid.DataBind();
}
基本上:
[HttpPost]
public ActionResult BugJqGridDataRequested()
{
using (var db = new BugContext()) {
var bugs = db.Bugs.Select(b => new { Prop1 = b.Prop, Prop2 = b.NavigationProperty.Data }).ToList();
return Json(new {
/// The number of pages which should be displayed in the paging controls at the bottom of the grid.
Total = 1,
/// The current page number which should be highlighted in the paging controls at the bottom of the grid.
Page = 1,
/// Anything serializable
/// UserData = null,
//The number of all available bugs not just the number of the returned rows!
Records = bugs.Count,
Rows = bugs
});
}
}
其他信息:我收到此错误
ObjectContext实例已被释放,无法再用于需要连接的操作。
然后我删除了使用,然后错误消失了现在我收到与jqgrid的JS文件有关的其他错误。。。但是您的情况似乎是这样的,因为Bug实体至少有一个导航属性,并且json序列化程序尝试将其序列化到,但是在序列化之前已经处理了DbContext,所以我们需要在构建结果集之前添加一个扁平类型。我已经编辑了我的答案。