【问题标题】:Not able to bind data with Kendo ui grid无法与 Kendo ui 网格绑定数据
【发布时间】:2017-07-27 17:59:05
【问题描述】:

这是我的操作方法

 public ActionResult Kendo([DataSourceRequest]DataSourceRequest request )
    {
        var emp = EmployeeManager.GetAllEmployees();

        DataSourceResult result = emp.ToDataSourceResult(request);
        return Json(result);
    }


这是我从官方网站获取的网格代码

@model IEnumerable<MyProject.Web.Models.EmployeeViewModels.EmployeeViewModel>

@using Kendo.Mvc.UI;

@(Html.Kendo().Grid<TalentPro.Employees.Employee>()
      .Name("grid")
      .DataSource(dataSource => dataSource //Configure the Grid data source.
          .Ajax() //Specify that Ajax binding is used.
          .Read(read => read.Action("Kendo", "Home")
          ) //Set the action method which will return the data in JSON format.

       )
      .Columns(columns =>
      {
          //Create a column bound to the ProductID property.
          columns.Bound(product => product.Id);
          //Create a column bound to the ProductName property.
          columns.Bound(product => product.FirstName);
          //Create a column bound to the UnitsInStock property.
          columns.Bound(product => product.LastName);
          columns.Bound(product => product.EmailId);
          columns.Bound(product => product.PhoneNumber);
      })
      .Pageable() // Enable paging
      .Sortable() // Enable sorting

)

我浏览了官方文档,它帮助我将 Kendo ui 与我的 Asp.net 核心 项目集成。但我不知道我哪里出错了,它没有将数据与网格绑定。

我尝试了多种方法,但没有用。谁能帮我解决这个问题。
提前致谢。

【问题讨论】:

    标签: kendo-ui kendo-grid asp.net-core-1.1


    【解决方案1】:

    终于找到了解决方案这些是我所做的更改

    1. 将 ActionResult 更改为 JsonResult
    2. 在 startup.cs 中添加了下面一行
      “.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());”
      感谢科比

    【讨论】:

      【解决方案2】:

      在你的控制器方法中,替换

      return Json(result);
      

      与

      return Json(result, JsonRequestBehavior.AllowGet);
      

      出于安全原因,MVC 默认为 DenyGet,因此您必须手动将其设置为 AllowGet,否则将无法正确返回。

      请务必注意,如果正在使用的浏览器是旧版本(截至 2018 年 1 月已修复),则返回的 JSON 对象可能会暴露一个小漏洞。仅当您传递特别敏感的信息并且您的用户能够从过时的浏览器访问该页面时,这才应该是一个问题。

      您可以阅读更多关于主题HERE 和HERE。

      【讨论】:

      • JsonRequestBehavior 在核心 1.0 中已贬值根据某些人的建议,我将 ActionResult 更改为 JsonResult 。但是没用
      • 抱歉,我没有注意到您使用的版本。您也可以尝试将返回类型替换为 IActionResult,并将 Json(result) 替换为 Ok(result)。 ActionResult、JsonResult 和 IActionResult 返回类型都应该与 Json 返回相同,所以我不再确定这是您的问题。
      • 您是否在控制器方法处设置了断点以查看它是否被调用?是否抛出任何错误?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多