【问题标题】:JqGrid is displayed with columns and no json dataJqG​​rid 显示有列,没有 json 数据
【发布时间】:2013-10-10 09:09:59
【问题描述】:

我正在尝试将 JSON 数据绑定到 jqgrid。但是,我没有得到任何数据。

这里是我的代码:

$(function () {
            $("#list").jqGrid({
                url:'<%:Url.Action("LoadData","Home")%>',
                datatype: "JSON",
                mtype: "GET",
                colNames: ["sid","sname"],
                colModel: [
            { name: "sid", width: 55,align:"center"},
            { name: "sname", width: 90,align:"center"},
                          ],
                jsonReader: {
                    repeatitems: false
                },
                pager: "#pager",
                rowNum: 10,
                rowList: [10, 20, 30],
                viewrecords: true,
                gridview: true,
                autoencode: true,
                caption: "My first grid"
            });
        }); 

我在 Asp.net MVC 应用程序中使用它。

我能够点击控制器并获取 JSON 数据..但我无法将数据显示到网格中。

我从控制器获得了正确的 json o/p。

我的控制器是:

public JsonResult LoadData()

   {
        var Data= new DataTable();
        Data= DataModel.LoadData();
        var JsonData = JsonConvert.SerializeObject(Packages, Formatting.Indented);
        return Json(new
        {
            JsonData
        }, JsonRequestBehavior.AllowGet);
    }

我认为我的 JQgrid jquery 代码中有错误。首先,我想以最少的配置实现 jqgrid。

我得到的 JSON 响应是:

[
  {
    "sid": 2,
    "sname": "ABC"
  },
  {
    "sid": 3,
    "sname": "XYZ"
  },
  {
    "sid": 4,
    "sname": "CBA"
  },
  {
    "sid": 5,
    "sname": "IIT"
  },
  {
    "sid": 6,
    "sname": "NIT"
  }
]

这是我的 HTML 结构:

<table id="list">
    </table> 
    <div id="pager"></div>

我从正在获取的数据中删除了重复项..

JSON 结果,我已经在 Visual Studio 的 Text Visualizer 中进行了检查。它很好..

请帮忙..

【问题讨论】:

  • 能否将LoadData返回的数据包括在内?您可以使用Fiddler、Firebug 或 IE 或 Chrome 的开发者工具来捕获确切的 HTTP 流量。顺便问一下sid 是否包含唯一 ID?
  • No..sid 有重复项
  • 我无法使用 Fiddler
  • 如果没有 fiddler,请尝试使用带有 firebug 或 chrome 控制台的 Firefox
  • 它不允许我下载任何东西..完全受限

标签: c# asp.net-mvc json jqgrid


【解决方案1】:

我想你的问题的原因是JsonConvert.SerializeObject 的使用。你应该返回 object 而不是 string。尝试使用

return Json(Packages, JsonRequestBehavior.AllowGet);

直接。

【讨论】:

  • 仍然..我得到空网格。但是,没有要查看的记录的消息消失了..现在
  • @Avinash:您使用哪个 jqGrid 版本?您使用当前版本的 jqGrid 还是旧的或复古的版本?如果使用旧版本,您可能需要添加jsonReader: {repeatitems: false, root: function (obj) { return obj; }}。此外,我建议您添加 loadonce: true 选项,但这不是您的主要问题的一部分。
  • 我使用的是4.5.4版本
  • 我需要包含grid.base.js吗?
  • @Avinash:您只需要包含grid.locale-en.jsquery.jqGrid.min.js(或query.jqGrid.src.js),请参阅the documentation
【解决方案2】:

不确定 .aspx 文件中是否有 .js 文件中的 js 代码。

无论如何试试这个并使用 Firefox/Chrome 控制台检查请求/响应

$(function () {
        $("#list").jqGrid({
            url:/Home/LoadData/,
            datatype: "json",
            mtype: "GET",
            colNames: ["sid","sname"],
            colModel: [
                      { name: "sid", width: 55,align:"center"},
                      { name: "sname", width: 90,align:"center"},
                      ],
            pager: "#pager",
            rowNum: 10,
            rowList: [10, 20, 30],
            viewrecords: true,
            gridview: true,              
            caption: "My first grid"
        });
    }); 

HTML

<table id="list"></table>
<div id="pager"></div>

【讨论】:

  • 我在 .aspx 页面中有它
  • @Avinash,那么如果您收到相同的回复,您的代码应该可以正常工作。
  • 你能检查一下我的控制器吗..返回类型应该是 JSONresult 或 ActionResult
  • @Avinash,JsonResultActionResult 的一种,所以这应该不是问题,你用对了
  • @Avinash 你的 html 结构是什么?检查也
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
  • 1970-01-01
  • 2011-09-10
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
  • 2011-03-11
相关资源
最近更新 更多