1

I have a kendo grid in MVC this is the declaration Html.Kendo().Grid(Model.Orders)

the object "Orders" has a list of "Details". I want to put this list in a second grid with the property ClientDetailTemplateId. demo

My question is, How I set the datasource of the template since the "Model" already has the data, in the example of the Hierarchy the datasource call an action in the controller

4

2 回答 2

4

我为此苦苦挣扎了很长一段时间,最终了解到这可以使用服务器绑定来完成。

关键似乎是 DetailTemplate 中可用的(据我所知,未记录的)“item”变量,它为您提供“主”网格上的当前行,其中包含应绑定到详细信息网格的数据(在您的情况下为“详细信息”)

这是来自 Kendo 的 ServerHierarchy 示例:

@model IEnumerable<Kendo.Mvc.Examples.Models.Employee>

@{ Html.Kendo().Grid(Model)
    .Name("Employees")
    .Columns(columns =>
    {
        columns.Bound(e => e.FirstName).Width(140);
        columns.Bound(e => e.LastName).Width(140);
        columns.Bound(e => e.Title).Width(200);
        columns.Bound(e => e.Country).Width(200);
        columns.Bound(e => e.City);
    })
    .DetailTemplate(
        @<text>           
            @(Html.Kendo().Grid(item.Orders)
                    .Name("Orders_" + item.EmployeeID)
                    .Columns(columns =>
                    {
                        columns.Bound(o => o.OrderID).Width(101);
                        columns.Bound(o => o.ShipCountry).Width(140);
                        columns.Bound(o => o.ShipAddress).Width(200);
                        columns.Bound(o => o.ShipName).Width(200);
                        columns.Bound(o => o.ShippedDate).Format("{0:d}");
                    })
                    .DataSource(dataSource => dataSource.Server())                    
                    .Pageable()
                    .Sortable()
                    .Filterable()
            )
        </text>
    )
    .RowAction(row => 
    {
        if (row.Index == 0)
        {
            row.DetailRow.Expanded = true;
        }
        else
        {
            var requestKeys = Request.QueryString.Keys.Cast<string>();
            var expanded = requestKeys.Any(key => key.StartsWith("Orders_" + row.DataItem.EmployeeID) ||
                key.StartsWith("OrderDetails_" + row.DataItem.EmployeeID));
            row.DetailRow.Expanded = expanded;
        }
    })
    .Pageable()
    .DataSource(dataSource => dataSource.Server().PageSize(5))    
    .Sortable()
    .Render();
} 
于 2013-10-02T13:56:19.797 回答
-1

在模板中而不是编写网格代码,调用局部视图并将订单模型传递给它

<script id="template" type="text/kendo-tmpl">
@Html.Partial("_Orders",Model.Order)
</script>

在这个局部视图中,使用模型作为Orders 模型编写剑道网格代码

局部视图

 @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>()
            .Name("grid_#=EmployeeID#")
            .Columns(columns =>
            {
                columns.Bound(o => o.OrderID).Width(70);
                columns.Bound(o => o.ShipCountry).Width(110);
                columns.Bound(o => o.ShipAddress);
                columns.Bound(o => o.ShipName).Width(200);
            })
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(5)
                .Read(read => read.Action("HierarchyBinding_Orders", "Grid", new { employeeID = "#=EmployeeID#" }))
            )
            .Pageable()
            .Sortable()
            .ToClientTemplate()
    )
于 2013-05-04T02:49:58.717 回答