Kendo Grid - Bind Data After Search

纵饮孤独 提交于 2019-12-22 07:06:20

问题


ASP.NET MVC 4 | .NET 4.5 | Razor| C#

I have a textbox and a button at the top of the page. Below that is a Kendo Grid which is bound to a List in my view model. When the user clicks the search button a jQuery ajax request is made and the data is returned as JSON. The only question I have is how do I bind that data to my Kendo Grid? Any help is appreciated.

@(Html.Kendo().Grid(Model.PurchaseOrder.LineItems)
    .Name("poSearchGrid")
    .Columns(c =>
    {
        c.Bound(x => x.LineNumber).Title("Line Number");
        c.Bound(x => x.Qty).Title("PO Qty");
        c.Bound(x => x.OpenQty).Title("Open Qty");
        c.Bound(x => x.QtyReceived).Title("Qty Received");
    })
    .Events(e => e.DataBound("onDataBound"))
    .DataSource(s => s.Ajax().Model(model => model.Id(i => i.ID)))
    )

Button Click Ajax Call

$("#btnSearch").on('click', function() {
console.log("click");
var searchText = $("#PONumber").val();

if (searchText == "") {
    alert("You must enter a search value");
    return;
}

    $.ajax({
        url: '@Url.Action("Search")',
        data: { poNumber: searchText},
        type: 'POST',
        dataType: "json",
        success: function(result) {

        }
    });
});

回答1:


All you need:

1- Your result object must be same type of Model.PurchaseOrder.LineItems and it must be a JSON object. If you use MVC ActionResult you can use below code in ServerCode:

return Json(lineItems);

2- Use below code in your succes ajax call:

var grid = $('#poSearchGrid').getKendoGrid(); //Or $('#poSearchGrid').data("kendoGrid");
grid.dataSource.data(result);
grid.refresh();


来源:https://stackoverflow.com/questions/19261998/kendo-grid-bind-data-after-search

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