Kendo UI Grid not populating after call to read

↘锁芯ラ 提交于 2019-12-11 04:24:58

问题


OK I have another and I'm sure I'm missing something simple again. Populating the Kendo Grid with a json result set. The load is triggered by a selection from a kendo dropdownlist control. I can see the data is returned from my webapi and converted to the json result. But the data is not displayed in the grid.

What I'm missing doing wrong?

Here is how grids view is added:

<div id="messageGridArea">
    @Html.Partial("Messages")
</div>

Here is dropdownlist definition:

@(Html.Kendo().DropDownList()
    .Name("importDates")
    .CascadeFrom("clients")
    .OptionLabel("Select Import Date...")
    .DataSource(source => {
        source.Read(read =>
        {
            read.Action("GetDistinctImportDates", "Home").Data("filterImportDates");
        })
        .ServerFiltering(true); 
     })
     .AutoBind(false)
     .Enable(false)
     .Events(e => e.Change("OnImportDatesChanged"))
)

Here is filter for grid parameters:

function filterGridMessages() {
    return { importdate: $("#importDates").val() };
}

Here is event to populate the grid:

function OnImportDatesChanged(e) {
    var grid = $("#clientMessages").data("kendoGrid");
    grid.dataSource.read();
}

Here is the grid definition:

@(Html.Kendo().Grid<Pulse.Data.Model.ImportHeader>()
    .Name("clientMessages")
    .AutoBind(false)
    .Filterable()
    .Groupable()
    .Sortable()
    .Pageable()
    .Scrollable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Model(model => model.Id(p => p.ImportId))
        .ServerOperation(false)
        .Read(read =>
            read.Action("GetImportMessages", "Home").Data("filterGridMessages")
         )
    )
    .Columns(columns =>
    {
        columns.Bound("ImportStatus").Title("I").Width("3%");
        columns.Bound("ImportTime").Title("Import Time");
        columns.Bound("RecordStatus").Title("Status").Filterable(true).Sortable(true).Width("7%");
        columns.Bound("RecordState").Title("State").Filterable(true).Sortable(true).Width("7%");
        columns.Bound("RecordAction").Title("Action").Filterable(true).Sortable(true).Width("7%");                
    })
)

And finally here is the controller code to read the data:

public JsonResult GetImportMessages(string importdate)
{
    IEnumerable<ImportHeader> messages = null;
    var msgs = client.GetStringAsync(string.Format("api/importheaders?$filter=RecordSource eq '{0}'", importdate)).Result;
    if (!string.IsNullOrWhiteSpace(msgs))
        messages = JsonConvert.DeserializeObject<IEnumerable<ImportHeader>>(msgs);
    return Json(messages, JsonRequestBehavior.AllowGet);
}

回答1:


A grid requires a DataSourceResult.

You should get something like this (have not tested it):

public JsonResult GetImportMessages([DataSourceRequest] DataSourceRequest request, string importdate)
{
    IEnumerable<ImportHeader> messages = null;

    var msgs = client.GetStringAsync(string.Format("api/importheaders?$filter=RecordSource eq '{0}'", importdate)).Result;
    if (!string.IsNullOrWhiteSpace(msgs))
        messages = JsonConvert.DeserializeObject<IEnumerable<ImportHeader>>(msgs);

    return Json(messages.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}


来源:https://stackoverflow.com/questions/18707806/kendo-ui-grid-not-populating-after-call-to-read

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!