Kendo Grid - 搜索后绑定数据 [英] Kendo Grid - Bind Data After Search

查看:16
本文介绍了Kendo Grid - 搜索后绑定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC 4 |.NET 4.5 |剃须刀|C#

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

我在页面顶部有一个文本框和一个按钮.下面是一个 Kendo Grid,它绑定到我的视图模型中的列表.当用户单击搜索按钮时,会发出一个 jQuery ajax 请求,并以 JSON 形式返回数据.我唯一的问题是如何将该数据绑定到我的 Kendo Grid?任何帮助表示赞赏.

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

按钮点击 Ajax 调用

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- 您的结果对象必须与 Model.PurchaseOrder.LineItems 类型相同,并且必须是 JSON 对象.如果您使用 MVC ActionResult,您可以在 ServerCode 中使用以下代码:

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- 在成功的 ajax 调用中使用以下代码:

2- Use below code in your succes ajax call:

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

这篇关于Kendo Grid - 搜索后绑定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆