Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo Grid - Bind Data After Search

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) {

        }
    });
});
like image 306
Will Avatar asked Oct 09 '13 02:10

Will


1 Answers

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();
like image 87
John Vu Avatar answered Sep 23 '22 01:09

John Vu