我目前正在尝试将json从控制器传递到jqgrid树。如果我使用嵌入在javascript变量中的JSON字符串,我能够正确显示树:
var TagsJson=
[
{
"id": "1","elementName": "Category: Arms/Pods", level:"0", parent:"", isLeaf:false, expanded:true, loaded:true
},
{
"id": "1_1","elementName": "AP | Active Pod Count (On an Arm)", level: "1", parent: "1", isLeaf: true, expanded: true, loaded: true
},
查看地点:
grid.jqGrid({
datastr: TagsJson,
datatype: "jsonstring",
mtype : 'POST',
height: "auto",
loadui: "disable",
colNames: ["Tag | Description"],
colModel: [
{name: "elementName", width:250, resizable: false}
],
treeGrid: true,
treeGridModel: "adjacency",
ExpandColumn: "elementName",
rowNum: 10000,
treeIcons: {leaf:'ui-icon-document-b'},
jsonReader: {
repeatitems: false,
root: function (obj) { return obj; },
records: function (obj) { return obj.length; }
}
});
我现在想要的是使用Controller传递数据而不是硬编码到变量:
我的控制器:
public JsonResult GetSpTreeJson()
{
var TagsJson = new
{
total = 1,
page = 1,
records = 2,
rows = new[]
{
new {id = 1, cell = new object[] {"1", "root 1", "0", "1", false, true, true}},
new {id = 11, cell = new object[] {"11", "child 11", "1", "1", true, true, true}}
}
};
return Json(TagsJson, JsonRequestBehavior.AllowGet);
}
我修改了View以从控制器获取json但没有任何反应,网格上没有显示数据但是在调试模式下有来自控制器的数据,下面是编辑的View,我做错了什么?
grid.jqGrid({
datatype: "json",
mtype: 'POST',
url: '/Demo/GetSpTreeJson/',
colNames: ["Tag | Description"],
colModel: [
{name: "elementName", width:250, resizable: false}
],
treeGrid: true,
treeGridModel: "adjacency",
ExpandColumn: "elementName",
rowNum: 10000,
treeIcons: {leaf:'ui-icon-document-b'},
jsonReader: {
repeatitems: false,
root: function (obj) { return obj; },
records: function (obj) { return obj.length; }
},
pager: "#list2",
caption: "Objects"
});