简体   繁体   English

jqGrid-从JSON加载值

[英]jqGrid - loading values from JSON

I have a jqGrid working OK using the local datatype, but I now want the values to be loaded via json but having trouble changing it. 我有一个jqGrid使用local数据类型jqGrid正常工作,但现在我希望通过json加载值,但无法更改它。

This is my jqGrid code 这是我的jqGrid代码

jQuery("#grid").jqGrid({
        datatype: "json",
        url: "/controller/getItems?id=2",
        width: 1405,
        colNames: ['id', 'surname'],
        colModel: [
            { name: 'id', index: 'id', editable: false, hidden: false, hidedlg: true },
            { name: 'surname', index: 'surname', editable: true }
        ],
        onSelectRow: function (id, status, e) {

        ...
    },
    editurl: url,

     ...

So the method to get the JSON is sucessfully fired. 因此,成功触发了获取JSON的方法。

    [HttpGet]
    public ActionResult getItems(string id)
    {

        List<model> items = method.getItems(id);
        string jsonText = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(items);
        return Json(jsonText, JsonRequestBehavior.AllowGet);

    }

The column names in the JSON do match the colModel names JSON的列名称与colModel名称匹配

Example of the json being returned - what the object jsonText above contains 返回json示例-上面的jsonText对象包含的内容

[{"id":434,"surname":"Woods"},
{"id":435,"surname":"Adams"}]

Is there anything I have done wrong or am missing? 我做错了什么或想念什么吗?

Thanks 谢谢

I suppose that the error in in using of System.Web.Script.Serialization.JavaScriptSerializer().Serialize . 我想在使用System.Web.Script.Serialization.JavaScriptSerializer().Serialize出现错误。 You need just return Json(items, JsonRequestBehavior.AllowGet); 您只需要返回Json(items, JsonRequestBehavior.AllowGet); . Additionally you can remove the column id from the colModel . 另外,您可以从colModel删除列id The id value will be still read and assigned as the value of id attribute of the rows ( id of <tr> elements of the grid) known as rowid. 仍将读取id值并将其分配为称为rowid的行的id属性值(网格的<tr>元素的id )。 You should add loadonce: true option to the grid because you don't implemented paging of data on the server side and to add gridview: true (if you not already use it) to have better performance and autoencode: true to interpret input data as texts instead of HTML fragments. 您应该向网格中添加loadonce: true选项,因为您没有在服务器端实现数据分页,而是添加了gridview: true (如果尚未使用)以提高性能,并自动autoencode: true以将输入数据解释为文本而不是HTML片段。

UPDATED : In case of usage old version of jqGrid one have to include jsonReader parameter which corresponds the format of input data: 更新 :如果使用旧版本的jqGrid,则必须包含jsonReader参数,该参数对应于输入数据的格式:

jsonReader: {
    repeatitems: false,
    root: function (obj) { return obj; }
}

One should still use loadonce: true option additionally. 仍然应该另外使用loadonce: true选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM