简体   繁体   English

如何正确绑定并用两个表EF填充Kendo UI网格?

[英]How can I properly bind and fill Kendo UI grid with two table EF?

My project has two main issues that prevent me from being able to do a normal, basic Kendo UI gridbind. 我的项目有两个主要问题,使我无法执行正常的基本Kendo UI Gridbind。 1. I do not have full access to the required SQL data (I cannot create a view) 2. One of the fields is incredibly large (the field overloads a ViewModel). 1.我没有对所需SQL数据的完全访问权限(我无法创建视图)。2.字段之一非常大(该字段使ViewModel重载)。

I was able to get my grid to work, but I feel that there is a better way to accomplish the task. 我能够使网格正常工作,但是我觉得有更好的方法来完成任务。 My problem, essentially, is that I can only seem to gridbind to a single table of DB. 本质上,我的问题是,我似乎只能网格绑定到单个数据库表。 To get passed this, I bound to the foreign key field and used a clientTemplate to fill the data from the read, but I feel like there should be a more direct way to be able to bind the field to the data. 为了获得通过,我绑定到外键字段,并使用clientTemplate填充读取的数据,但是我觉得应该有一种更直接的方法可以将字段绑定到数据。 My read data is an actionresult Linq join from EF, converted to JSON. 我读取的数据是来自EF的actionresult Linq连接,已转换为JSON。 Here is my code, if anyone can make a suggestion. 这是我的代码,如果有人可以提出建议。 D_Type from table 2 is what I am trying to bind directly to the column: 表2中的D_Type是我试图直接绑定到该列的内容:

@(Html.Kendo().Grid<ProjectName.Models.Table1>()
    .Name("grid")
    .Scrollable()
    .ColumnMenu()
    .Reorderable(reorder => reorder.Columns(true))
    .Columns(columns =>
    {

        columns.Bound(c => c.P_Name);
        columns.Bound(c => c.E_Name);
        //Cannot be directly bound to a column because it is part of Table2 table
        columns.Bound(c => c.TypeId).Title("Type").ClientTemplate("#= D_Type #").Filterable(false);            
        columns.Command(command => command.Custom("Details").Click("showDetails")).Title("Results").Width(80);
        columns.Bound(c => c.Destination);
        columns.Bound(c => c.Location);
        columns.Bound(c => c.Version);
        columns.Bound(c => c.StartDt);
        columns.Bound(c => c.EndDt);
        columns.Bound(c => c.StartDt).Title("Duration").ClientTemplate("#: calcDuration(StartDt, EndDt) #").Sortable(false).Filterable(false);
        columns.Bound(c => c.);
        columns.Bound(c => c.MiscNotes).ClientTemplate("#: errorDisplay(ResultObject) # ").Sortable(false).Filterable(false);


    })
    //Style dictates full grid height
    .HtmlAttributes(new { style = "height: 725px;" })
    .Filterable()
    .Resizable(resize => resize.Columns(true))
    .Sortable(sortable => sortable
        .AllowUnsort(true)
        .SortMode(GridSortMode.SingleColumn))
    .Pageable(pageable => pageable
        .Refresh(false)
        .PageSizes(true)
        .ButtonCount(5))
    .Events(e => e.DataBound("onDataBound"))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Sort(sort => sort.Add("StartDt").Descending())
        .Read(read => read.Action("Project_Read", "Grid"))
        .PageSize(20)
        .ServerOperation(true)
    )
    )

Don't bind your grid to ProjectName.Models.Table1. 不要将网格绑定到ProjectName.Models.Table1。

Instead, bind it to a ViewModel that contains the fields from Table1 and the field from Table2 that you want to display in the Grid, ie 而是将其绑定到包含要在Grid中显示的Table1字段和Table2字段的ViewModel,即

public class SpecificGridViewModel
{
    public string P_Name { get; set; }
    // Other fields you want from Table1
    // ...
    // PLUS the field(s) you want from the other, joined table.
    public string D_Type { get; set; }
}

Then you have your Project_Read method return a list of these ViewModels that you have constructed from your LINQ query instead of a list of Table1s. 然后,让您的Project_Read方法返回从LINQ查询构造的这些ViewModel的列表,而不是Table1的列表。

ie write a LINQ query that returns the exact data you want(data from Table1 joined with Table2.D_Type via Table1.TypeID) and return a result set of custom ViewModel that you create to hold the data from the query and then bind your grid to that model instead of the "raw" Table1 model. 即编写一个LINQ查询,该查询返回所需的确切数据(来自Table1的数据通过Table1.TypeID与Table2.D_Type联接),并返回一个自定义ViewModel的结果集,该结果集是您创建的,用于保存查询中的数据,然后将网格绑定到该模型,而不是“原始” Table1模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM