【问题标题】:Load data for jqgrid treegrid using json doesn't work使用 json 为 jqgrid treegrid 加载数据不起作用
【发布时间】:2014-10-13 21:25:18
【问题描述】:

我只是尝试在我的网站中使用 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]}]
}

【问题讨论】:

  • 你能检查请求(它接受application/json)和响应吗?将响应 json 粘贴到您的问题中。
  • 除了类型不匹配(字符串而不是整数)之外,它似乎没问题。你可以在适当的地方尝试使用整数(级别和父 ID)吗?
  • 不,还是不行 :( new json: { "total":1,"page":1,"records":2, "rows": [{"id":1,"单元格":[1,"根 1",0,null,false,false,true]},{"id":11,"cell":[11,"child 11",1,1,true,false,真]}]}

标签: asp.net-mvc json jqgrid mvcjqgrid


【解决方案1】:

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

function DataTypeCorrection() {
    $("#tc-jqgrid").setGridParam({ datatype: 'json' });
}

【讨论】:

    猜你喜欢
    • 2017-11-28
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多