0

Jqgrid 没有显示 JSON 数据,但是正在生成行

服务器端代码:

public JsonResult Denominations()
{
.
.
int counter = 0;
var jsonData = new
{
total = result.UserObject.Count,
page = 1,
rows = (
      from p in result.UserObject
      select new
      {
            id = ++counter,
            cell = new string [] { 
                   p.CurrencyID.ToString(), 
                   p.DenominationID.ToString(), 
                   p.DenominationName.ToString(), 
                   p.DenominatorCount.ToString(), 
                   p.Multiplier.ToString(), 
                   p.TenderID.ToString()
                     }
                 }).ToArray()

            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
}

来自服务器端的数据是这样的: {"total":1,"page":1,"rows":[{"id":1,"cell":["1","1","Penny", "0","0.0100","1"]}]}

JavaScript 代码:

$("#denominators").jqGrid({
        url: '/Denominations?tenderid=1&currencyid=1',
        contentType: "application/json",
        datatype: "json",
        jsonReader: {
            root: 'rows',
            page: 'page',
            total: 'total',
            repeatitems: false,
            cell: 'cell',
            id: 'id',
            userdata:'userdata'
        },
        mtype: "GET",
        colNames: ["CurrencyID", "DenominationID", "TenderID", "Multiplier", "DenominationName", "DenominatorCount"],
        colModel: [
            { name: "currencyid", width: 80, align: "center" },
            { name: "denominationid", width: 90, align: "center" },
            { name: "tenderid", width: 250 },
            { name: "multiplier", width: 250 },
            { name: "denominationname", width: 95 },
            { name: "denominatorcount", width: 95 },
        ],
        height: 'auto',
        loadonce: true,
        sortname: "DenominationID",
        sortorder: "desc",
        viewrecords: true,
        gridview: true,
        autoencode: true
    });

看法:

<table id="denominators" ></table>

View 创建带有列标题的网格,但是生成了行,但行没有任何数据 int。

4

1 回答 1

0

你用错了jsonReader。确切地说,该属性repeatitems: false是错误的。这意味着rows数组中每个项目的格式是

{
    "currencyid": "1",
    "denominationid": "1",
    "tenderid": 1,
    "denominationname": "Penny",
    "denominatorcount": "0",
    "multiplier": "0.0100"
}

你用

{
    "id": 1,
    "cell": [
        "1",
        "1",
        "Penny",
        "0",
        "0.0100",
        "1"
    ]
}

反而。所以你应该删除jsonReader,因为输入数据的格式对应于默认值jsonReader,但你仍然需要重新排序网格的列或更改你放置在cell数组中的项目的顺序,以便它对应于列的顺序colModel

附加说明:您对 . 使用了错误的值total。应该是页数。顺便说一句,您使用loadonce: true. 在这种情况下,您可以"total":1,"page":1从响应中删除部分并只返回命名项目的数组。如果项目,您应该只选择与属性名称相同的列名称。

于 2015-06-19T09:05:31.407 回答