I'm trying to follow the example for populating the grid with remote data. I've setup my controller to accept the DataSourceRequest param and, per the example, am getting the data from the DB and then calling ToDataSourceResult - which I assume will do the skip/take/count.
A little more detail:
I'm making a call to the DB using EF and calling an SP (not with "using db context" around call so the connection stays open after I return to the controller so the controller can do the skip/take/count). Once the DB call is complete, I have ObjectResult<MyEntity_Result>. I then select from this ObjectResult<> into an anonymous type and then call ToDataSourceResult. The error I get is: "The result of a query cannot be enumerated more than once."
Now, if I call ToList() after I have selected my anonymous type from the ObjectResult<>, but before I call ToDataSourceResult, everything works well. But, why would I want to call ToList(), especially if I have tons of data?
Second issue: Once I put ToList() on there and am getting data back, the grid isn't showing the data. When I return the data without the ToDataSourceResult, and do my own skip/take I get data (but of course, I can't get count in addition to the skip/take since it will enumerate twice and throw an exception). And if I do my own skip/take, the paging doesn't show more than one page when I know there are at least 10,000 records.
Here is the AJAX Controller Action method:
Here is the js that is output:
A little more detail:
I'm making a call to the DB using EF and calling an SP (not with "using db context" around call so the connection stays open after I return to the controller so the controller can do the skip/take/count). Once the DB call is complete, I have ObjectResult<MyEntity_Result>. I then select from this ObjectResult<> into an anonymous type and then call ToDataSourceResult. The error I get is: "The result of a query cannot be enumerated more than once."
Now, if I call ToList() after I have selected my anonymous type from the ObjectResult<>, but before I call ToDataSourceResult, everything works well. But, why would I want to call ToList(), especially if I have tons of data?
Second issue: Once I put ToList() on there and am getting data back, the grid isn't showing the data. When I return the data without the ToDataSourceResult, and do my own skip/take I get data (but of course, I can't get count in addition to the skip/take since it will enumerate twice and throw an exception). And if I do my own skip/take, the paging doesn't show more than one page when I know there are at least 10,000 records.
Here is the AJAX Controller Action method:
public
JsonResult LoadAllUsers([DataSourceRequest]DataSourceRequest request)
{
var repo =
new
AdminRepository();
var users = repo.LoadAllUsers();
var resp = users.Select(u =>
new
{
u.UserId,
u.UserName,
u.FirstName,
u.LastName,
u.EmailAddress,
u.LastActivityDate,
});
var data = resp.ToList().ToDataSourceResult(request);
// var data = resp.Skip(20).Take(20).ToList();
return
Json(data, JsonRequestBehavior.AllowGet);
}
Here is the js that is output:
$(
'#grid'
).kendoGrid({dataSource: {transport: {read:
'/AdminJson/LoadAllUsers'
},schema: {model: {fields: {UserId: { type:
'number'
},UserName: { type:
'string'
},FirstName: { type:
'string'
},LastName: { type:
'string'
},EmailAddress: { type:
'string'
},LastActivityDate: { type:
'date'
}}}},pageSize: 20,serverPaging:
true
,serverFiltering:
false
,serverSorting:
true
,scrollable:
true
},height: 430,pageable:
true
,filterable:
false
,sortable:
true
,columns: [{ field:
'UserId'
, title:
'User ID'
, sortable:
false
, filterable:
false
, groupable:
false
},{ field:
'UserName'
, title:
'User Name'
, sortable:
true
, filterable:
false
, groupable:
false
},{ field:
'FirstName'
, title:
'First Name'
, sortable:
true
, filterable:
false
, groupable:
false
},{ field:
'LastName'
, title:
'Last Name'
, sortable:
true
, filterable:
false
, groupable:
false
},{ field:
'EmailAddress'
, title:
'Email Address'
, sortable:
true
, filterable:
false
, groupable:
false
},{ field:
'LastActivityDate'
, title:
'Last Activity Date'
, sortable:
true
, filterable:
false
, groupable:
false
, format:
'{0: yyyy-MM-dd HH:mm:ss}'
}]});