【问题标题】:Kendo Grid not reading json data?Kendo Grid 不读取 json 数据?
【发布时间】:2015-05-27 08:44:55
【问题描述】:

这是我用来在 kendo 上执行 crud 的 javascript 代码,记住我正在使用 api 调用,在测试中我必须只使用 json 数据

  document.onreadystatechange = function () {



var viewModel = kendo.observable({

    products: new kendo.data.DataSource({


        transport: {

            read: {
              type: "GET",
                url: "/api/Companies/GetAllCompanies2",
                dataType: "json"
            },

            create: {

                 type: "PUT",
                    url: "/api/Companies/UpdateDefCompny",
                  contentType: "application/json; charset=utf-8",
                    dataType: "json",
                async: false

            },
            update: {
                url:"/api/Companies/SaveDefCompny",
                async: false,
                contentType: "application/json",
                dataType: "json",
                type: "POST"
                 // here you need correct api url

            },
            destroy: {
                url: "/api/Companies/Delete", // here you need correct api url
                dataType: "json"
            },


            parameterMap: function (data, operation) {
                if (operation !== "read" && data) {
                    return JSON.stringify(data.models[0]);
                }
            }
        },
        serverPaging: true,
        serverFiltering: true,
        pageSize: 10,
        schema: {
            //data:"Data",
            total: "Count",

            model: {
                id: "Id",
                fields: {
                    Id: { type: "int" },
                    CurrentCurrencyCode: { editable: true, type: "int" },
                    ShortName: { editable: true, type: "string" },
                    FullName: { editable: true, type: "string" },
                    ContactPerson: { editable: true, type: "string" },
                    Address1: { editable: true, type: "string" },
                    CompanyCity: { editable: true, type: "string" },
                    CompanyState: { editable: true, type: "string" },
                    CompanyCountry: { editable: true, type: "string" },
                    ZipPostCode: { editable: true, type: "string" },
                    TelArea: { editable: true, type: "string" }

                }
            }
        },
        batch: true,


    }) 


});


   kendo.bind(document.getElementById("example"), viewModel);

  }

这是我的服务器端代码,它返回 json 数据,我已经测试过,它成功返回了 json 数据,我确实需要它

   [HttpGet]
    public string GetAllCompanies2()
    {
        List<object> myCo = DefCompany.AllDefCompany2;


        object json = JsonConvert.SerializeObject(myCo);
        return json.ToString();

    }

网格代码:

  <div id="example">
    <div id="kendoGrid"
         data-role="grid"
         data-pageable=" true"
         data-sortable=" true"
         data-filterable="true"
         data-toolbar="['create','save', 'cancel']"
         data-editable="inline"
         data-columns="[

      { 'field': 'Id', 'width': 100 },
           { 'field': 'CurrentCurrencyCode', 'width': 100 },
              { 'field': 'ShortName', 'width': 100 },
          { 'field': 'FullName', 'width': 100 },
       { 'field': 'ContactPerson', 'width': 100 },
         { 'field': 'Address1', 'width': 100 },
       { 'field': 'CompanyCity', 'width': 100 },
         { 'field': 'CompanyState', 'width': 100 },
          { 'field': 'CompanyCountry', 'width': 100 },
          { 'field': 'ZipPostCode', 'width': 100 },
      { 'field': 'TelArea', 'width': 100 },
      { command: ['edit'], title: 'Actions', width: '250px' },

     ]"
         data-bind="source: products"
         style=" height :500px"></div>
</div>
<div>






</div>

这里的问题是,为什么剑道网格没有填充 json 数据,它只在我反序列化或返回对象时填充,但我必须用 json 填充它?

【问题讨论】:

标签: c# json kendo-ui json.net kendo-grid


【解决方案1】:

在 global.aspx.cs 文件中

     using System.Data.Entity;

  namespace RpManticSolAPI
  {
  public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);          
    }
}
   }

【讨论】:

    【解决方案2】:

    而不是这个:

    [HttpGet]
    public string GetAllCompanies2()
    {
        List<object> myCo = DefCompany.AllDefCompany2;
    
    
        object json = JsonConvert.SerializeObject(myCo);
        return json.ToString();
    
    }
    

    这样做:

    [HttpGet]
    public List<object> GetAllCompanies2()
    {
        List<object> myCo = DefCompany.AllDefCompany2;
        return myCo;
    }
    

    然后,您的数据将以无需解析的纯 json 形式返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多