ToDataSourceResult loses Data

4 posts, 1 answers
  1. Eric
    Eric avatar
    24 posts
    Member since:
    Dec 2010

    Posted 29 Apr 2020 Link to this post

    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();
  2. Answer
    Anton Mironov
    Admin
    Anton Mironov avatar
    210 posts

    Posted 30 Apr 2020 Link to this post

    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.
  3. Eric
    Eric avatar
    24 posts
    Member since:
    Dec 2010

    Posted 30 Apr 2020 in reply to Anton Mironov Link to this post

    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.
  4. Anton Mironov
    Admin
    Anton Mironov avatar
    210 posts

    Posted 04 May 2020 Link to this post

    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.
Back to Top