如何根据Viewbag值有条件地分组或不分组KendoUI网格

时间:2013-04-25 14:59:46

标签: asp.net-mvc kendo-ui kendo-grid viewbag

我需要将列名或值'none'传递给我的kendo网格,以根据Viewbag元素的值有条件地对其进行分组。当我按预期传递组中的列名称时。我的问题是如果传入值'none'则不进行分组。我所拥有的代码是:

@(Html.Kendo().Grid<dynamic>()
    .Name("exportGrid")
    .DataSource(dataSource =>
    {
        dataSource.Ajax()
        .Read("ReadGrid", "Report", new { id = Model.Inquiry_ID })
        .Group(grp => grp.Add(ViewBag.groupBy, typeof(string)))
        .Model(m =>
        {
            // Add the fields to the dynamic model
            foreach (var field in Fields)
            {
                switch (field.DATA_TYP_NUM)
                {
                    case 1: m.Field(field.INTERNL_NME, typeof(string)); break;
                    case 2: m.Field(field.INTERNL_NME, typeof(double?)); break;
                    case 3: m.Field(field.INTERNL_NME, typeof(double?)); break;
                    case 4: m.Field(field.INTERNL_NME, typeof(DateTime?)); break;
                }
            }
        })

        .ServerOperation(true);
    })
    .Groupable()
    .Filterable()
    .Sortable()
    .ColumnMenu()
    .Events(e => e.DataBound("onDataBound"))
    .Resizable(resize => resize.Columns(true))
    .Columns(columns =>

正如我所说 - 这很好但我需要一种在.Group(....)

时排除Viewbag.groupBy == "none"子句的方法

1 个答案:

答案 0 :(得分:6)

只需在Group选项中添加条件:

.Group(grp => {
    if(ViewBag.groupBy != "none") {
        grp.Add(ViewBag.groupBy, typeof(string));
    }
})