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

ToDataSourceResult loses Data

3 Answers 954 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Veteran
Iron
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
1
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.
Frank
Top achievements
Rank 1
Iron
commented on 20 Sep 2023, 02:43 PM

@Anton Mironov

Hello, I hope this reaches you because my company bought your controls and they are great but I cannot seem to find an example of my exact needs.

I have an app with a grid, controller, and data and I am returning it as shown above but there is a requirement where a user on a different page will select a row and redirect to my page sending me a value to apply as a filter on a column as if it were done by the user.

When the view is returned, and the grid rendered, I want the column's filter icon to be lit up because of the filter I added.

I found this code snippet on another thread:

request.Filters.Add(new FilterDescriptor() { Member = "InsuredName", MemberType = typeof(string), Operator = FilterOperator.Contains, Value = "" });

but I cannot use this method if I am returning the data as shown above and not using ToDataSourceResult(request)

Thanks,

Frank

Ivan Danchev
Telerik team
commented on 22 Sep 2023, 06:48 AM

Frank,

Review the following demo: https://demos.telerik.com/aspnet-mvc/grid/customajaxbinding In the demo's View Source tab you can see the server side logic, in particular the CustomAjaxBinding_Read action, in which filtering is applied to the data like this:

orders = orders.ApplyOrdersFiltering(request.Filters);
The ApplyOrdersFiltering method is declared at the end of the controller:

public static IQueryable<OrderViewModel> ApplyOrdersFiltering(this IQueryable<OrderViewModel> data,
           IList<IFilterDescriptor> filterDescriptors)
        {
            if (filterDescriptors != null && filterDescriptors.Any())
            {
                data = data.Where(ExpressionBuilder.Expression<OrderViewModel>(filterDescriptors, false));
            }
            return data;
        }

After applying filtering, sorting, paging, grouping, the orders collection is passed to the DataSourceResult() instance:

IEnumerable data =  orders.ApplyOrdersGrouping(request.Groups);

var result = new DataSourceResult()
{
    Data = data,
    Total = total
 };

return Json(result);

Frank
Top achievements
Rank 1
Iron
commented on 22 Sep 2023, 12:09 PM

Thanks! This is very helpful.

-Frank

0
Eric
Top achievements
Rank 1
Veteran
Iron
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
Iron
Answers by
Anton Mironov
Telerik team
Eric
Top achievements
Rank 1
Veteran
Iron
Share this question
or