繁体   English   中英

从ASP.NET返回的JSON不与Kendo网格绑定

[英]JSON returned from ASP.NET does not bind with Kendo grid

我可以使用以下代码成功对Web方法进行AJAX调用,并且Web方法返回粘贴在下面的JSON:

我的网络方法

[WebMethod]
public static string GetJson()
{
     string query = "SELECT top 10 cast(ClientUID as varchar) ClientUID FROM <tablename>";
     SqlCommand cmd = new SqlCommand(query);
     // Populate the DataSet.
     DataSet data = GetData(cmd);
     // return the Customers table as JSON.
     DataTable dt = data.Tables[0];
     var returnJSON = (from p in dt.AsEnumerable()
          select new
          {
               ClientUID = p.Field<string>("ClientUID")
           }).ToList();
      var serializer = new JavaScriptSerializer();
      string json = serializer.Serialize(returnJSON);
      return json; 
}

Web方法返回的JSON:

[{“ ClientUID”:“ 1”},{“ ClientUID”:“ 2”},{“ ClientUID”:“ 3”},{“ ClientUID”:“ 4”},{“ ClientUID”:“ 5”} ,{“ ClientUID”:“ 6”},{“ ClientUID”:“ 7”},{“ ClientUID”:“ 8”},{“ ClientUID”:“ 9”},{“ ClientUID”:“ 10”} ]

使用AJAX调用Web方法

<script type="text/javascript">
        $(document).ready(function() {
            $.ajax(
                    {
                        type: "POST",
                        url: "ServeClientCalls.aspx/GetJson",
                        data: {},
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        async: true,
                        cache: false,
                        success: function(msg) {
                        //checking the content return from the above web method
                        $("#msg").html(msg.d); 
                            //Binding to kendo Grid
                            $("#grid").kendoGrid({
                                dataSource: {
                                    data: msg.d,
                                    schema: {
                                        model: {
                                            fields: {
                                                ClientUID: { type: "string" }
                                            }
                                        }
                                    },
                                    pageSize: 20
                                },

                                height: 430,
                                filterable: true,
                                sortable: true,
                                pageable: true,
                                columns: [
                                        { field: "ClientUID" }
                                    ]
                            });
                        },
                        error: function(x, e) {
                            $("#msg").html(x.responseText);
                        }
                    });
        });
    </script>

问题:我的网格未绑定,仅显示标题,而当我以下面提到的方式使用代码时,它正在工作

<script type="text/javascript">
        $(document).ready(function() {
            $.ajax(
                    {
                        type: "POST",
                        url: "ServeClientCalls.aspx/GetJson",
                        data: {},
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        async: true,
                        cache: false,
                        success: function(msg) {
                        //checking the content return from the above web method
                        $("#msg").html(msg.d);
                        **var p = [{ ClientUID: 1 }, { ClientUID: 2 }, { ClientUID: 3 }, { ClientUID: 4 }, { ClientUID: 5 }, { ClientUID: 6 }
                            , { ClientUID: 7 }, { ClientUID: 8 }
                            , { ClientUID: 9 }, { ClientUID: 10}];**
                            //Binding to kendo Grid
                            $("#grid").kendoGrid({
                                dataSource: {
                                    **data: p,**
                                    schema: {
                                        model: {
                                            fields: {
                                                ClientUID: { type: "string" }
                                            }
                                        }
                                    },
                                    pageSize: 20
                                },

                                height: 430,
                                filterable: true,
                                sortable: true,
                                pageable: true,
                                columns: [
                                        { field: "ClientUID" }
                                    ]
                            });
                        },
                        error: function(x, e) {
                            $("#msg").html(x.responseText);
                        }
                    });
        });
    </script>

模式部分下使用数据:“ d” 那应该工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM