在ASP MVC中将List <string>传递给Kendo网格

时间:2018-07-09 18:03:22

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

我有一个Kendo网格,其中一列传递了List<string>。现在它显示[object Object],但是我希望它显示用逗号分隔的字符串列表。我找到了这个答案:Kendo Grid: How display List<string> in one cell?

但是提出的解决方案对我不起作用。它只是阻止了网格的渲染。

这将在列中显示[object Object]

                     @(Html.Kendo().Grid<Models.MyConfigModel>
                         ()
                         .Name("MyListGrid")
                         .Columns(columns =>
                         {
                            columns.Bound(p => p.myList).Title("My List").HeaderHtmlAttributes(new { style = "overflow: visible; white-space: normal;text-align:center" }).Width(150);
                         })
                        .DataSource(dataSource => dataSource
                        .Ajax()
                        .PageSize(100)
                        .Read(read => read.Action("MyListGrid_Read", Model.ControllerName))
                        .ServerOperation(false).Model(model => model.Id(p => p.MyID)))

这不会显示任何内容:

                     @(Html.Kendo().Grid<Models.MyConfigModel>
                         ()
                         .Name("MyListGrid")
                         .Columns(columns =>
                         {
                            columns.Bound(p => p.myList).Title("My List").ClientTemplate("# =iterate(myList) #").HeaderHtmlAttributes(new { style = "overflow: visible; white-space: normal;text-align:center" }).Width(150);
                         })
                        .DataSource(dataSource => dataSource
                        .Ajax()
                        .PageSize(100)
                        .Read(read => read.Action("MyListGrid_Read", Model.ControllerName))
                        .ServerOperation(false).Model(model => model.Id(p => p.MyID)))

这是我的迭代函数:

function iterate(object) {
    if (object !== null && object.length > 0) {
        for (var x = 0; x < object.length; x++) {
            html += object[x];
            html += ", ";
        }
        html = html.slice(0, -2);
        return html;
    }
}

1 个答案:

答案 0 :(得分:0)

这是我要怎么做:

网格:

columns.Template(p => { }).Title("My List").ClientTemplate("#= iterate(data) #").HeaderHtmlAttributes(new { style = "overflow: visible; white-space: normal;text-align:center" }).Width(150);

Javascript:

function iterate(data) {
    if (data.myList!== null && data.myList.length > 0) {
        for (var x = 0; x < data.myList.length; x++) {
            html += data.myList[x];
            html += ", ";
        }
        html = html.slice(0, -2);
        return html;
    }
    return "";
}