javascript - jqGrid - 从 JSON 加载值

标签 javascript c# jquery json jqgrid

我有一个使用 local 数据类型工作正常的 jqGrid,但我现在希望通过 json 加载值,但在更改时遇到问题它。

这是我的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,

     ...

因此,获取 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);

    }

JSON 中的列名称与 colModel 名称匹配

返回的 json 示例 - 上面的对象 jsonText 包含的内容

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

我是否做错了什么或遗漏了什么?

谢谢

最佳答案

我认为使用 System.Web.Script.Serialization.JavaScriptSerializer().Serialize 时出错。您只需返回 Json(items, JsonRequestBehavior.AllowGet); 。此外,您可以删除列 id来自colModelid value 仍将被读取并分配为 id 的值行的属性(网格的 id 元素的 <tr>)称为 rowid。您应该添加loadonce: true网格选项,因为您没有在服务器端实现数据分页并添加 gridview: true (如果您还没有使用它)以获得更好的性能和 autoencode: true将输入数据解释为文本而不是 HTML 片段。

更新:如果使用旧版本的 jqGrid,则必须包含 jsonReader对应输入数据格式的参数:

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

仍应使用loadonce: true另外的选项。

关于javascript - jqGrid - 从 JSON 加载值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27276370/

相关文章:

javascript - 使用js显示元素,初始状态隐藏

javascript - PWA : Address bar showing up when redirectioning to subdomains

javascript - 为什么 Firefox 不允许您在主框架中执行脚本?

c# - 如何在 C# 中获取呈现的 HTML 文档的高度

javascript - INT 到十进制 JavaScript

javascript - 将 st、nd、rd 和 th(序数)后缀添加到数字

javascript - 为什么我的 if、else if 和 else 语句不能正常工作?

c# - 如何在最短时间内插入1000万条记录?

c# - 如何避免在 C# 中重复 "object.method()"?

javascript - 用js统计字符串中<span>的字符数