Reputation: 501
I am using ASP.NET MVC4 and the MVC wrappers for Kendo UI (2013.2.830). My goal is to get server side paging/sorting/filtering/grouping all working together with the Kendo grid. I can get the paging/sorting/filtering working fine... but need some advice on how to handle the grouping. Here is what I have so far for the action that is called from the grid:
public ActionResult GetGridDataSource([DataSourceRequest] DataSourceRequest request)
{
// get the data using the values from the request
EntityCollection<CustomerEntity> customers;
int totalItemCount;
using (var proxy = new CustomerServiceProxy())
{
// NOTE: just an example...
// The service doesn't take in the kendo data types for sort/filters.
// They are transformed to data types that the service does use.
customers = proxy.FindCustomers(
request.Filters,
request.Sorts,
request.Page,
request.PageSize,
out totalItemCount);
}
// build the datasource using the view model
var dataSource = (from customer in customers
select new CustomerViewModel
{
CustomerName = customer.Name,
// etc
}).ToList();
// this code returns the data for the requested page correctly
var result = new DataSourceResult();
result.Total = totalItemCount;
result.Data = dataSource;
return Json(result, JsonRequestBehavior.AllowGet);
}
If I use var result = dataSource.ToDataSourceResult(request);
then everything works fine except paging (no data is returned back to the client after page 1). It appears that the ToDataSourceResult() method is trying to extract page 2 from dataSource even though the dataSource already represents page 2.
Ideally, I would like to use ToDataSourceResult() IF the paging issue can be resolved. Otherwise, I assume I will need to manually apply the grouping to my datasource before I set the result.Data property so the JSON returned is in the correct format. Is there a Kendo function available to do this? If I need to do it manually can someone provide an example of how to take the grouping arguments from the request and apply it to the datasource so the JSON is correct?
Thanks!
Upvotes: 2
Views: 5541
Reputation: 404
My answer provided here explains a server-side approach that has worked great for paging, filtering, sorting and (the most elusive) grouping, all using the Kendo.Mvc
.NET library from Telerik.
Bear in mind that my answer makes use of a JavaScript-declared Kendo DataSource, rather than with MVC wrappers, as it gave me better granular control over all DataSource options when I first wrote my solution. However, you'll find more about the MVC wrapper approach here, as well as, with using the MVC wrappers against a WebApi
backend here.
Upvotes: 0
Reputation: 748
Try this:
public virtual JsonResult GetGridDataSource([DataSourceRequest()] DataSourceRequest request)
//Get data : var dataSource = ...
int requestPage = request.Page;
request.Page = 1;
var result = dataSource.ToDataSourceResult(request);
result.Total = sortpag.ItemCount;
request.Page = requestPage;
return Json(result);
}
Upvotes: 0
Reputation: 9820
See KendoGridBinderEx on nuget or github which supports sorting, filtering, paging , grouping and aggregates.
Upvotes: 1