无法绑定jqGrid中的json数据

时间:2011-02-25 11:41:55

标签: jquery wcf json jqgrid grid

$(document).ready(function () {
    jQuery("#tblGridDemo").jqGrid({
        url: 'GridDataService.svc/GetMyGridData',
        datatype: 'json',
        height: 250,
        type: "GET",
        width: 920,
        contentType: 'application/json; charset=utf-8',
        colNames: ['Id', 'Name','Organization'],
        colModel: [
            { name: 'id', index: 'id', width: 60 },
            { name: 'Name', index: 'Name', width: 90 },
            { name: 'Organization', index: 'Organization', width: 100 }
        ],
        multiselect: true,
        caption: "Bind service Data",
        rowNum: 10,
        pager: jQuery("#divPagerDemo"),
        rowList: [10, 15, 20, 30, 50, 100],
        sortname: 'Id',
        sortorder: "desc",
        loadtext: "Loading....",
        shrinkToFit: true,
        emptyrecords: "No records to view",
        rownumbers: true,
        viewrecords: true,
        sucess: function (a) {
            alert(a);
        }
    });
    jQuery("#tblGridDemo").jqGrid('navGrid', '#divPagerDemo',
                                  { edit: false, add: false, del: false });
});

---服务

[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public List<GridDataFormat> GetMyGridData()
{
    List<GridData> lstGridData = new List<GridData>();           
    List<GridDataFormat> lstGridDataFormat = new List<GridDataFormat>();
    try
    {
        for (int iCountRows = 1; iCountRows <= 10; iCountRows++)
        {
            GridData objGridData = new GridData();
            objGridData.Id = iCountRows;
            objGridData.Name ="Name"+iCountRows;
            objGridData.Organization = "Organization" + iCountRows;
            lstGridData.Add(objGridData);
        }

        int total = 10, page = 1;

        GridDataFormat obj = new GridDataFormat();
        obj.total = total;
        obj.page = page;
        obj.records = 10;
        obj.rows = lstGridData;              
        lstGridDataFormat.Add(obj);
    }
    catch (Exception ex)
    {
    }
    return lstGridDataFormat;
}

----- lstGridDataFormat.cs

[DataContract]
[Serializable]
public class GridDataFormat
{
    [DataMember]
    public int page;

    [DataMember]
    public int total;

    [DataMember]
    public int records;

    [DataMember]
    public List<GridData> rows;
}

1 个答案:

答案 0 :(得分:0)

您必须使用serializeGridData(请参阅here),例如

serializeRowData: function(data) { return JSON.stringify(data); }

another answer中,您会找到一个代码示例,您可以根据需要下载该代码示例。