3

Here is my Action Method

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

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


This is my grid code which i have taken from official website

@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

)

I have gone through official documentation it helped me to integrate Kendo ui with my Asp.net core Project. But i have no clue where i went wrong, its not binding the data with the grid.

I have been trying multiple ways but no use. Can anyone help me out to solve this issue.
Thanks in Advance.

2 Answers2

2

Finally Got solution These are the changes i made

  1. changed ActionResult to JsonResult
  2. One more below line added in startup.cs
    ".AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());"
    Thanks for coby
1

In your controller method, replace

return Json(result);

with

return Json(result, JsonRequestBehavior.AllowGet);

MVC defaults to DenyGet for security reasons, so you have to manually set it to AllowGet, or it won't return correctly.

It's important to note that this can expose a small vulnerability to your JSON object being returned if the browser being used is an older version (It has been fixed as of ). This should only be a concern if you're passing particularly sensitive information AND your users are able to access the page from outdated browsers.

You can read more on the subject HERE and HERE.

Coby G.
  • 73
  • 1
  • 9
  • JsonRequestBehavior has depreciated in core 1.0 As per some one's suggestion i changed ActionResult to JsonResult . But no use – Natarajan Nagarajugari Jul 28 '17 at 05:03
  • I apologize, I didn't notice the version you were using. You can also try replacing return type with IActionResult and replacing Json(result) with Ok(result). ActionResult, JsonResult, and IActionResult return types should all work the same with Json return, so I'm no longer sure that is your problem. – Coby G. Jul 28 '17 at 14:18
  • Have you set a breakpoint at the controller method to see that it is being called? Are any errors being thrown? – Coby G. Jul 28 '17 at 14:24