我创建了一个带有两个表的测试sql server数据库:workitem和workitemnote。它们之间有外键,因此一个工作项可以有多个工作项注释记录。
我试图在剑道网格中显示工作项。操作返回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);
     }
 }

从我的操作可以看出,我甚至对网格的workitemnotes都不感兴趣。
为了证明这是由于这种关系,我创建了第二个操作,它可以工作并显示数据:
  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);
      }
  }

有办法解决这个问题吗?

最佳答案

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

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>

09-10 05:25