【问题标题】:Kendo Grid is not displaying json data webformsKendo Grid 不显示 json 数据网络表单
【发布时间】:2016-07-15 20:59:07
【问题描述】:

我是剑道网格的新手,我正在使用 webmethod 来触发它。在很多论坛的帮助下,我终于能够触发对 webmethod 的请求。问题是我的方法返回 JSON 格式的记录数。为简单起见,我认为这里的一条硬编码 JSON 记录是响应:

{"d":"{\"pk_Picture\":22,\"P_DisplayOrder\":1,\"AltAttribute\":\"Smith\",\"TitleAttribute\":\"Smith\"}"}

<script>
                $(document).ready(function () {

                    $("#productpictures-grid").kendoGrid({
                        height: 200,
                        columns: [
                            { field: "pk_Picture", title: "Picture",  width: "150px" },
                            { field: "P_DisplayOrder", width: "150px" },
                            { field: "AltAttribute", width: "100px" },
                            { field: "TitleAttribute", width: "100px" },
                            { command: "destroy", title: "Delete", width: "110px" }
                        ],
                        pageable: {
                            info: true
                        }, // enable paging
                        //filterable: true, // enable filtering
                        //sortable: true, // enable sorting
                        editable: true, // enable editing
                        //toolbar: ["create", "save", "cancel"], // specify toolbar commands
                        dataSource: {
                            serverPaging: true,
                            serverSorting: true,
                            serverFiltering: true,
                            pageSize: 10,
                            schema: {
                                data: "d.results", // web methods return JSON in the following format { "d": <result> }. Specify how to get the result.
                                total: "d.Total",
                                model: { // define the model of the data source. Required for validation and property types.
                                    fields: {
                                        pk_Picture: { editable: false, type: "string" },
                                        P_DisplayOrder: { editable: true, type: "string" },
                                        AltAttribute: { editable: true, type: "string" },
                                        TitleAttribute: { editable: true, type: "string" }
                                    }
                                }
                            },
                           // batch: true, // enable batch editing - changes will be saved when the user clicks the "Save changes" button
                            transport: {
                                create: {
                                    url: "TestPage.aspx/Create", //specify the URL which should create new records. This is the Create method of the Products.asmx service.
                                    contentType: "application/json; charset=utf-8", // tells the web method to serialize JSON
                                    type: "POST" //use HTTP POST request as the default GET is not allowed for web methods
                                },
                                read: {
                                    url: "Update.aspx/FillProductDataById", //specify the URL which data should return the records. This is the Read method of the Products.asmx service.
                                    contentType: "application/json; charset=utf-8", // tells the web method to serialize JSON
                                    type: "POST" //use HTTP POST request as the default GET is not allowed for web methods
                                },
                                update: {
                                    url: "TestPage.aspx/Update", //specify the URL from which should update the records. This is the Update method of the Products.asmx service.
                                    contentType: "application/json; charset=utf-8", // tells the web method to serialize JSON
                                    type: "POST" //use HTTP POST request as the default GET is not allowed for web methods
                                },
                                destroy: {
                                    url: "TestPage.aspx/Destroy", //specify the URL which should destroy records. This is the Destroy method of the Products.asmx service.
                                    contentType: "application/json; charset=utf-8", // tells the web method to serialize JSON
                                    type: "POST" //use HTTP POST request as the default GET is not allowed for web methods
                                },
                                parameterMap: function (data, operation) {
                                    if (data.models) {
                                        return JSON.stringify(data);
                                        return JSON.stringify({ products: data.models });
                                    } else if (operation == "read") {
                                        //Page methods always need values for their parameters
                                        data = $.extend({  ProductIds: $('#Id').val() }, data);
                                        return JSON.stringify(data);
                                    }
                                }
                            }
                        }
                    });
                });
            </script>

这是我的服务器方法

    [WebMethod]
public static string FillProductDataById(string ProductIds)
{
    BLProduct objProduct = new BLProduct();
    string json = JsonConvert.SerializeObject(new { pk_Picture = 22, P_DisplayOrder = 1, AltAttribute = "Smith", TitleAttribute = "Smith" });
    return json;
    //DataTable dt = new DataTable();
    //dt = objProduct.GetProductByProductId(ProductIds);
    //return JsonConvert.SerializeObject(dt);
}

请帮忙

【问题讨论】:

    标签: jquery asp.net json webforms kendo-grid


    【解决方案1】:

    您从服务器返回的 JSON 似乎不是数组。 从服务器到剑道的 JSON 数据应该是一个数组。

    例如:

    [{"pk_Picture":22,"P_DisplayOrder":1,"AltAttribute":"Smith","TitleAttribute":"Smith"},
    {{"pk_Picture":33,"P_DisplayOrder":2,"AltAttribute":"Hamed","TitleAttribute":"Hamed"}]
    

    这样做:

    [WebMethod]
    public static string FillProductDataById(string ProductIds)
    {
        BLProduct objProduct = new BLProduct();
        DataTable dt = new DataTable();
        dt = objProduct.GetProductByProductId(ProductIds);
        var dataArray = dt.AsEnumerable().ToArray();
        return JsonConvert.SerializeObject(dataArray);
    }
    

    【讨论】:

    • 那我该怎么办
    • 再次检查答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多