我的问题与此类似:
Binding Kendo Data Source with Async $.ajax calling from C# MVC Controller Action
这是用于创建Kendo网格的javascript:
$(document).ready(function () {
$("#grid").kendoGrid({
dataSource: getData(),
height: 550,
groupable: true,
sortable: true,
pageable: {
refresh: true,
pageSizes: true,
buttonCount: 10
},
columns: [{
field: "a",
title: "a",
width: 200
}, {
field: "a",
title: "a"
}]
});
});
这是我用来获取数据的getData函数,它是一个ajax回调函数,它在名为doSearch的控制器中调用一个动作
function getData() {
jQuery.ajax({
type: "POST",
url: "@Url.Action("doSearch")",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ needSearch: true }),
success: function (data) {
var dataSource = new kendo.data.DataSource({
data: JSON.parse(data)
});
dataSource.read();
return dataSource;
},
failure: function (errMsg) {
alert(errMsg);
}
});
}
回调很好,并且“数据”具有值,但是问题是:网格中没有显示数据。
这是数据的返回值:
[{"a": "First record"},{"a": "Second record"}]
代码有什么问题?为什么没有显示数据?
================================================== ===============
这是getData函数的更新代码:
function getData() {
var grid = $("#grid").data("kendoGrid");
grid.dataSource.transport.read.url = '@Url.Action("doSearch",new {needSearch = true})';
grid.dataSource.read();
}
这是动作:
public ActionResult doSearch(bool needSearch)
{
return Json("[{\"a\": \"First record\"},{\"a\": \"Second record\"}]");
}
但是Controller中的doSearch操作没有触发
最佳答案
我尝试了您的方式,但这对我有用:
function getData() {
var grid = $("#grid").data("kendoGrid");
grid.dataSource.transport.options.read.url = '@Url.Action("doSearch", new {needSearch = true})';
grid.dataSource.read();
}
备用:
$("#grid").kendoGrid({
dataSource: {
type: "json",
transport: {
read: '@Url.Action("doSearch", new {needSearch = true})'
}
}
);
注意:根据您的编辑,您的控制器方法需要更改为
public ActionResult doSearch(bool needSearch)
{
return Json(new[] { new { a = "First record" }, new { a = "Second record" }};
}
关于javascript - 使用Ajax回调将数据源绑定(bind)到Controller时,数据未在Kendo网格上显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29645836/