0

我只是尝试在我的网站中使用 jqgrid。我正在使用vs 2013,asp.net mvc 5,并从nuget安装了jqgrid和jqgridmvc(jqgrid mvc helper)。我按照 jqgridmvc 制造商的示例(使用 xml 作为源),它工作正常: http ://mvcjqgrid.skaele.it/Home/TreeGrid

但是,当我尝试使用 json 作为源时,不知何故它不起作用。我怀疑我的 json 格式是错误的,但我不确定在哪里:

控制器代码:

public JsonResult TreeGridData(GridSettings gridSettings)
  {
  var jsonData = new
    {
    total = 1,
    page = 1,
    records = 2,
    rows = new[]{
      new {id = 1, cell = new object[] {"1", "root 1", "0", null, false, false, true}},
      new {id = 11, cell = new object[] {"11", "child 11", "1", "1", true,false, true}}
      }
    };
 return Json(jsonData, JsonRequestBehavior.AllowGet);
 }

查看代码:

@(Html.Grid("treeGrid")
.SetCaption("TreeGrid")
.AddColumn(new Column("ItemID")
  .SetLabel("Id").SetKey(true))
.AddColumn(new Column("Name")
  .SetAsExpandable())
.SetUrl(Url.Action("TreeGridData"))
.EnableTreeGrid()
 )

谢谢!

编辑:根据评论中的建议,我添加了从控制器生成的响应(www.localhost .../controller/treegriddata):

{
"total":1,
"page":1,
"records":2,
"rows":
[{"id":1,"cell":["1","root 1","0",null,false,false,true]},
 {"id":11,"cell":["11","child 11","1","1",true,false,true]}]
}
4

1 回答 1

0

好的,原来是因为我使用的库(mvcjqgrid)不支持我从 Nuget 下载的最新版本的 jqgrid。生成的网格代码是“treedatatype”,而对于较新的 jqgrid,正确的代码是“datatype”。所以我只是在 onBeforeRequest 事件中运行下面的函数,一切正常!

function DataTypeCorrection() {
    $("#tc-jqgrid").setGridParam({ datatype: 'json' });
}
于 2014-08-27T22:48:31.420 回答