This is a migrated thread and some comments may be shown as answers.
ToDataSourceResult loses Data
3 Answers 63 Views
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Veteran
Eric asked on 29 Apr 2020, 12:22 PM

There must be something about the ToDataSourceResult method that I do not understand.  When DataSourceRequest.Page is 1, the resulting DataSourceResult.Data has the 10 elements that I passed into it, but, if DataSourceRequest.Page is 2, the resulting DataSourceResult.Data has 0 elements.  It is as if ToDataSourceResult is doing paging by only selecting the elements for the selected page range, but I am doing my own paging, so I want all elements passed to ToDataSourceResult to be included in DataSourceResult.Data.  Does this mean I should not be using ToDataSourceResult and should just populate the Data field manually?

 

request.Page = 1;
// results is a list of 10 elements
var gridModel = results.ToDataSourceResult(request);
// since Page is 1, dataCount will be 10
var dataCount = gridModel.Data.AsQueryable().Count();
 
request.Page = 2;
// results is a list of 10 elements
gridModel = results.ToDataSourceResult(request);
// since Page is 2, dataCount will be 0
dataCount = gridModel.Data.AsQueryable().Count();

3 Answers, 1 is accepted

Sort by
0
Accepted
Anton Mironov
Telerik team
answered on 30 Apr 2020, 01:50 PM

Hello Eric,

Thank you for the provided details.

The ToDataSourceResult() method applies paging internally. Therefore, it would try to apply paging on the already paged data. What I can recommend is that you return a custom instance of the DataSourceResult class. Here is an example:

public ActionResult Remote_Binding_Orders_Read([DataSourceRequest]DataSourceRequest request)
        {
            //Apply custom paging to collection and pass it to the DataSource result object
            var result = new DataSourceResult()
            {
                Data = collection,
                Total = clientInput.Count()
            };

            return Json(result);
        }

A live demo demonstrating this functionality could be found here:

https://demos.telerik.com/aspnet-core/grid/customajaxbinding

Let me know if you need further assistance.

 

Regards,
Anton Mironov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Eric
Top achievements
Rank 1
Veteran
answered on 30 Apr 2020, 01:56 PM
I am glad to hear that you have an example in the Core demos, but I am still using the original ASP.Net MVC 5 controls.  It would be nice if this was documented in those demos, as well.  I have not been looking at the Core demos, because I assumed the functionality would not be exactly the same.
0
Anton Mironov
Telerik team
answered on 04 May 2020, 08:28 AM

Hi Eric,

It is correct that the same approach could be applied in the ASP.NET MVC framework. Based on the information in your first reply, I was not sure which framework was in use. Nevertheless, check out the corresponding live demo here:

https://demos.telerik.com/aspnet-mvc/grid

Let me know if there's anything else I can help you with.

Best Regards,
Anton Mironov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Grid
Asked by
Eric
Top achievements
Rank 1
Veteran
Answers by
Anton Mironov
Telerik team
Eric
Top achievements
Rank 1
Veteran
Share this question
or