jqGrid没有将JSON数据呈现到网格中 [英] jqGrid not rendering JSON data into grid

查看:104
本文介绍了jqGrid没有将JSON数据呈现到网格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我的操作方法以JSON格式返回数据,但是jqGrid控件无法呈现它.

Even though my action method is returning data in JSON format, however, the jqGrid control is not able to render it.

这是以JSON格式返回数据的方法的代码.

Here's the code of the method that returns data in JSON format.

ContactContext db = new ContactContext();

    //
    // GET: /Contact/

    public JsonResult ContactList(int? selectedContact)
    {

        IQueryable<Contact> contacts = db.Contacts;

        var contactsJson = JsonConvert.SerializeObject(contacts.ToList());

        return Json(contactsJson, JsonRequestBehavior.AllowGet);
    }

由Controller类的"ContactList"操作方法返回的数据:

Data returned by 'ContactList' action method of Controller class:

        contactsJson    ""[{\"ContactId\":1,\"FirstName\":\"John\",\"LastName\":\"Doe\",\"EMail\":\"john.doe@gmail.com\",\"Phone\":\"7458593847\",\"BusinessName\":\"microsoft\"},{\"ContactId\":2,\"FirstName\":\"Jack\",\"LastName\":\"Davos\",\"EMail\":\"jack.davos@microsoft.com\",\"Phone\":\"348945485\",\"BusinessName\":\"microsoft\"},{\"ContactId\":3,\"FirstName\":\"Mike\",\"LastName\":\"Strong\",\"EMail\":\"mike.strong@google.com\",\"Phone\":\"950595959\",\"BusinessName\":\"google\"}]"

jqGrid代码:

function populateContactList() {

    $("#ContactTable").jqGrid({
        url: "/Contact/ContactList",
        datatype: "json",
        colNames: ["ID", "First Name", "Last Name", "EMail", "Phone", "Business Name"],
        colModel: [
            { name: "ContactId", index: "ContactId", width: 80 },
            { name: "FirstName", index: "FirstName", width: 200 },
            { name: "LastName", index: "LastName", width: 200 },
            { name: "EMail", index: "EMail", width: 300 },
            { name: "Phone", index: "Phone", width: 100 },
            { name: "BusinessName", index: "BusinessName", width: 200 },
        ],
        //data: result,
        mtype: 'GET',
        autowidth: true, 
        shrinkToFit: false,
        //loadonce: true,
        viewrecords: true,
        gridview: true,
        emptyrecords: "No records to display",
        jsonReader: {
            repeatitems: false,
            //page: function () { return 1; },
            root: function (obj) { return obj; },
            //records: function (obj) { return obj.length; }
        },
        loadComplete: function () {},
        loadError: function (jqXHR, textStatus, errorThrown) {
            alert('HTTP status code: ' + jqXHR.status + '\n' +
                'textstatus: ' + textstatus + '\n' +
                'errorThrown: ' + errorThrown);
            alert('HTTP message body  (jqXHR.responseText: ' + '\n' + jqXHR.responseText);
        }
    });
}

$(document).ready(populateContactList);

推荐答案

我认为您的数据已序列化两次,

I think your data is serialized twice,

一行,

var contactsJson = JsonConvert.SerializeObject(contacts.ToList());

和第二次来自JsonResult动作过滤器

and second time from JsonResult action filter

return Json(contactsJson, JsonRequestBehavior.AllowGet);

发送json数据,例如

sending json data like,

return Json(contacts.ToList(), JsonRequestBehavior.AllowGet);

代替

var contactsJson = JsonConvert.SerializeObject(contacts.ToList());

return Json(contactsJson, JsonRequestBehavior.AllowGet);

应该工作.

希望这会有所帮助.

这篇关于jqGrid没有将JSON数据呈现到网格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆