When I was passing LINQ to Entities queries directly to the model for the grid it would only work properly with server binding. Any attempt to add AJAX support resulted in an exception from the JavaScriptSerializer complaining about circular references. After much research I've come to find that this is most likely a limitation of the JavaScriptSerializer and not related to the Telerik controls.
Following advice on Resolving circular references when binding the MVC Grid I created a set of view models to map the entities to something the JavaScriptSerializer can work with. Now the problem is that sorting, paging, filtering, and so on are all broken.
What is the proper way to pass data into the model for the grid to consume using the setup described above? More information any samples of my classes (they're super simple...) can be found on my related Stack Overflow post: Entity Framework Code First Dynamic Proxy Navigation Properties.
9 Answers, 1 is accepted
What do you mean by saying that sorting and paging are broken? Is there chance to attach a sample project so we can investigate?
The grid should work as expected with view models provided it is setup as in the blog post.
Atanas Korchev
the Telerik team
private IEnumerable<Member> DoNotContactList { get { return from member in this.Entities.MemberRepository.DoNotContact select new Member(member); }}In the above, the property DoNotContact of MemberRepository returns the EF IQueryable. The new Member(member) is mapping the EF entity to a view model of the same name.
If you look at the comments on the blog post about using view models, Makoto writes:
"With the ViewModel approach, won't you lose the built in Linq expression engine?
Wouldn't we have to implement our own custom paging/sorting?
That's what I'm running into at least. If I don't implement my own paging/sorting, then my application queries for EVERY record in the database."
That's exactly the problem I'm having now. The exception I get is "The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'."
This looks like a different problem. EF needs OrderBy to be issues before Skip(). The grid has some code which checks for EF and emits an OrderBy if there isn't one. Which version of Telerik Extensions for ASP.NET MVC are you using? We had a problem with CodeFirst EF which I think we have addressed.
Regards,Atanas Korchev
the Telerik team
Could you please try with the attached build? If it still fails - please send me your project. I think this is not a problem with the view model approach but a known issue which we currently do not detect and circumvent.
Regards,Atanas Korchev
the Telerik team
I've tried applying the paging and sorting before passing the model into the grid control. The problem with this is that the grid control then thinks the data set is only as large as the page I've given it.
Is there any other way around this?
If you don't want to send us your project then you can try implementing custom binding as shown here.
Greetings,Atanas Korchev
the Telerik team
Thanks, I suppose I'll have to go this route for now. I appreciate your very prompt responses to my questions.
Falls on deaf ears because thwy unfortunatley don;t seem to build stuff with their own technologies - they just sell it - so they only find out the issues later on.