2017-07-27 52 views
2

这里是我的行动方法不能与剑道UI网格数据绑定

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 

我已经通过它帮助我剑道UI与我Asp.net核心项目整合官方文档了。但我不知道我出错的地方,它没有将数据与网格绑定。

我一直在尝试多种方式,但没有用。任何人都可以帮我解决这个问题。
在此先感谢。

回答

2

终于得到溶液这是我提出

  1. 改变的ActionResult到JsonResult变化
  2. 一个更下面线在startup.cs加入
    ” .AddJsonOptions(选项=> options.SerializerSettings.ContractResolver = new DefaultContractResolver());“
    感谢coby
1

在你的控制器的方法,代替

return Json(result); 

return Json(result, JsonRequestBehavior.AllowGet); 

MVC默认DenyGet出于安全原因,所以你必须手动将其设置为AllowGet,否则将不会返回正确。

重要的是要注意,如果浏览器使用的是旧版本(它已被修复),这可能会导致您的JSON对象返回一个小的漏洞。如果您传递的是特别敏感的信息,并且您的用户能够从过时的浏览器访问该页面,则这应该只是一个问题。

您可以在主题HEREHERE上阅读更多内容。

+0

JsonRequestBehavior在核心1.0中已折旧根据某人的建议,我将ActionResult更改为JsonResult。但没有用 –

+0

我很抱歉,我没有注意到您使用的版本。 您也可以尝试用IActionResult替换返回类型,并用Ok(结果)替换Json(result)。 ActionResult,JsonResult和IActionResult返回类型都应该与Json return一样工作,所以我不再确定那是你的问题。 –

+0

您是否在控制器方法中设置了断点以查看它正在被调用?是否有错误被抛出? –