如何使用asmx Web服务C#绑定jqGrid

时间:2012-03-10 08:59:32

标签: json web-services jqgrid asmx

我需要一些关于如何将jqGrid与asmx web服务C#绑定的帮助,我找到了一些关于如何将asmx web服务转换为JSON的主题但是我不清楚

此致

1 个答案:

答案 0 :(得分:2)

首先,您应该定义WebMethod,它将为jqGrid提供数据。如果您计划实施服务器端排序和分页,则webmethod应至少具有以下参数

public JqGridData TestMethod (int page, int rows, string sidx, string sord)

其中JqGridData类将被定义,例如

public class TableRow {
    public int id { get; set; }
    public List<string> cell { get; set; }
}
public class JqGridData {
    public int total { get; set; }
    public int page { get; set; }
    public int records { get; set; }
    public List<TableRow> rows { get; set; }
}

还有其他不同的选择如何填充网格,但首先要了解至少一种方法。

重要的是,要从Web方法返回JSON数据,您不需要手动将返回的数据转换为JSON 。您只需要使用数据返回对象,ASMX Web服务将根据HTTP请求的标头将对象本身序列化为XML或JSON。

对服务器的请求将在HTTP标头的application/json; charset=utf-8部分中包含application/jsonContent-Type,返回的数据将为JSON,并且

{
    "d": {
        "page": 1,
        "total": 4,
        "records": 4,
        "rows": [
            ...
        ]
    }
}

在客户端,您应该使用

$("#list").jqGrid({
    url: 'MyTestWS.asmx/TestMethod',
    datatype: 'json',
    mtype: 'POST',
    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
    serializeGridData: function (postData) {
        return JSON.stringify(postData);
    },
    jsonReader: {
        root: "d.rows",
        page: "d.page",
        total: "d.total",
        records: "d.records"
    }
    gridview: true,
    ...
}

有关代码示例,请参阅here

更新Herehere您可以下载Visual Studio演示项目。有关其他演示项目的更多链接,请参阅the answer

相关问题