This is a migrated thread and some comments may be shown as answers.

Ajax grid paging not paging serverside

1 Answer 12 Views
Grid
This is a migrated thread and some comments may be shown as answers.
marcellus
Top achievements
Rank 1
marcellus asked on 19 Aug 2020, 03:59 PM
 

I am attempting to have an ajax grid that does server side paging. (For reference, I am using Entity Framework for db context, and Automapper for model to view model mapping). The paging is working clientside, but it just pulls in all the data from my db then pages. I cannot seem to get the paging to reflect at the sql query level without doing it manually (which defeats the purpose of passing the request to the DataSourceResult(). Am i missing something, or must i do this manually?

 

Below is my current code:

 

Razor page

01.@(Html.Kendo()
02.            .Grid<AgencyContactViewModel>()
03.            .Name("Grid")
04.            .DataSource(dataSource => dataSource
05.                .Ajax()
06.                .Read(read => read.Action("Read", "AgencyContact"))
07.                .PageSize(5)
08.                .ServerOperation(true)
09.       )
10. 
11.        .Columns(columns =>
12.         {
13.             columns.Bound(o => o.ID).Width(50).Title("Id");
14.             columns.Bound(o => o.LastName).Width(100).Title("Last Name");
15.             columns.Bound(o => o.FirstName).Width(100).Title("First Name");
16.             columns.Bound(o => o.AgencyDescription).Width(175).Title("Agency");
17.             columns.Bound(o => o.IsActiveFlag)
18.             .ClientTemplate("<input type='checkbox' #= IsActiveFlag ? checked = 'checked' : '' #  disabled='disabled' ></input>")
19.             .Width(15).Title("Active");
20. 
21.             columns.Bound(o => o.AgencyCode)
22.             .ClientTemplate("<a href='" + Url.Action("Edit", "AgencyContact") + "/#=AgencyCode#'>Edit<a/>")
23.             .Title("")
24.             .Width(15);
25.             columns.Bound(o => o.AgencyCode)
26.             .ClientTemplate("<a href='" + Url.Action("Details", "AgencyContact") + "/#=AgencyCode#'>Details<a/>")
27.             .Title("")
28.             .Width(15);
29.         })
30.        .Groupable()
31.        .Sortable()
32.        .Pageable()
33.        .Filterable())
 
Controller
1.public JsonResult Read([DataSourceRequest] DataSourceRequest request)
2.{
3.    var agencyContacts = dbContext.AgencyContacts.Include("Agency") .OrderBy(a => a.AgencyCode);
4.    var agencyContactsViewModel = agencyContacts.ProjectTo<AgencyContactViewModel>();
5.    var result = agencyContactsViewModel.ToDataSourceResult(request);
6.    return Json(result, JsonRequestBehavior.AllowGet);
7.}

1 Answer, 1 is accepted

Sort by
0
Veselin Tsvetanov
Telerik team
answered on 21 Aug 2020, 09:55 AM

Hi Marcellus,

As far as I can understand, you need to apply the paging within the SQL query. If that is the case, you will need to call the ToDataSourceResult(request) before mapping the db Model to the ViewModel:

var agencyContacts = dbContext.AgencyContacts.Include("Agency").OrderBy(a => a.AgencyCode);
var result = agencyContacts.ToDataSourceResult(request);
result.Data = result.Data.Cast<AgencyContact>().ProjectTo<AgencyContactViewModel>();

return Json(result, JsonRequestBehavior.AllowGet);

Any logic executed after AutoMapping (ProjectTo<>) on the mapped collection will be executed on the server and not within the query itself.

Regards,
Veselin Tsvetanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
marcellus
Top achievements
Rank 1
Answers by
Veselin Tsvetanov
Telerik team
Share this question
or