如何将Entity Framework 6绑定到KendoUI Grid

时间:2014-10-14 15:18:32

标签: c# .net entity-framework kendo-ui kendo-grid

我创建了一个带有两个表的测试SQL Server数据库:WorkItem和WorkItemNote。它们之间有一个外键,一个WorkItem可以有许多WorkItemNote记录。 我试图在Kendo网格中显示WorkItems。 Action返回Json,但网格中没有显示任何内容 如果我只使用我想要显示的属性创建一个匿名对象,它就可以工作。 这表示具有关系(WorkItemNote)的实体(WorkItem)无法在Kendo网格中显示。
有没有办法解决这个问题?

我的网格:

<div>
    @(Html.Kendo().Grid<TryToLoadGrid.Models.WorkItem>()
        .Name("grid")
        .HtmlAttributes(new { style = "height:380px;" } )
        .Sortable()
        .Pageable()
        .DataSource(dataSource => dataSource.Ajax().Read(read => read.Action("GetActiveWorkItems", "Home")))
        .Columns(columns =>
            {
                columns.Bound(workItem => workItem.Title);
                columns.Bound(workItem => workItem.Description);
            }
        )
    )
</div>

我的操作代码是:

 public ActionResult GetActiveWorkItems([DataSourceRequest]DataSourceRequest request)
 {
     using (TaskManagerContext context = new TaskManagerContext())
     {
         IQueryable<WorkItem> list = context.WorkItems.Where(x => x.IsActive == true);
         DataSourceResult result = list.ToDataSourceResult(request);
         return Json(result);
     }
 }

从我的Action中可以看出,我甚至对网格的WorkItemNotes不感兴趣。

作为证明这是由于这种关系,我创建了第二个Action,它可以工作并显示数据:

  public ActionResult GetActiveWorkItems2([DataSourceRequest]DataSourceRequest request)
  {
      using (TaskManagerContext context = new TaskManagerContext())
      {
          List<WorkItem> list = context.WorkItems.Where(x => x.IsActive == true).ToList();
          List<Object> dataObjects = new List<object>();
          foreach (WorkItem workItem in list)
          {
              dataObjects.Add(new { Title = workItem.Title, Description = workItem.Description });
          }

          DataSourceResult result = dataObjects.ToDataSourceResult(request);
          return Json(result);
      }
  }

有什么方法吗?

1 个答案:

答案 0 :(得分:4)

我个人认为直接在UI中使用Entity Framework模型是一种不好的做法。您应该从模型中提取所需的数据并将其放在视图模型中。然后将您的视图模型列表返回给客户端。

public class WorkItemViewModel 
{
    public Int64 ID { get; set; }
    public string Title { get; set }
    public string Desctription { get; set }
}


public ActionResult GetActiveWorkItems2([DataSourceRequest]DataSourceRequest request)
{
    using (TaskManagerContext context = new TaskManagerContext())
    {
        List<WorkItem> list = context.WorkItems.Where(x => x.IsActive == true).ToList();
        List<WorkItemViewModel> dataObjects = new List<WorkItemViewModel>();
        foreach (WorkItem workItem in list)
        {
            dataObjects.Add(new WorkItemViewModel { Title = workItem.Title, Description = workItem.Description });
        }
        return Json(dataObjects.ToDataSourceResult(request));
    }
}


<div>
    @(Html.Kendo().Grid<ViewModelNamespace.WorkItemViewModel>()
        .Name("grid")
        .HtmlAttributes(new { style = "height:380px;" } )
        .Sortable()
        .Pageable()
        .DataSource(dataSource => dataSource.Ajax().Read(read => read.Action("GetActiveWorkItems", "Home")))
        .Columns(columns =>
            {
                columns.Bound(workItem => workItem.ID).Hidden();
                columns.Bound(workItem => workItem.Title);
                columns.Bound(workItem => workItem.Description);
            }
        )
    )
</div>