问题
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