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

[Solved] Ajax binding issue with ViewModel and Grid Expression Egine

4 Answers 83 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Justin Bigelow
Top achievements
Rank 1
Justin Bigelow asked on 13 Jan 2010, 03:55 PM
I've run into a problem with the Telerik Grid Expression builder and the Ajax binding support. My model is based on an EF 4 Object Set. If I have an IQueryable<Appointment> and use this as the data source for my grid then I get the DB side paging (which I need since the table is ~64,000 records). However since the Appointment entity has a relationship to SalesRep I get a circular reference error when serializing IQueryaby<Appointment> to JSON (there is no actual circular reference SalesRep to Appointment is a standard parent/child relationship.)

I was able to get around the JSON serialization error by iterating over my collection and converting it into a ViewModel collection:

        [Telerik.Web.Mvc.GridAction] 
        public ActionResult _AjaxBinding() 
        { 
            AppointmentRepository appointmentRepository = new AppointmentRepository(); 
            List<AppointmentGridItemViewModel> appointmentGridItems = new List<AppointmentGridItemViewModel>(); 
            IQueryable<Appointment> appointments = appointmentRepository.ListAppointments(); 
            foreach (Appointment app in appointments) 
            { 
                appointmentGridItems.Add(new AppointmentGridItemViewModel(app)); 
            } 
 
            return View(new Telerik.Web.Mvc.GridModel<AppointmentGridItemViewModel> 
            { 
                Data = appointmentGridItems 
            }); 
        } 

My problem now is that since I am now minding to IList instead of IQueryable the entire result set is being sent to View and paging is being managed there.

Is there any way to address the JSON serialization issue but still get database side paging?

Thank you,
Justin


4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 13 Jan 2010, 04:58 PM
Hi Justin Bigelow,

Could you please try with this code:

var appointmentGridItems = from a in appointmentRepository.ListAppointments()
                            select new AppointmentGridItemViewModel(a);
 
return View(new Telerik.Web.Mvc.GridModel<AppointmentGridItemViewModel>
{
    Data = appointmentGridItems
});

Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Justin Bigelow
Top achievements
Rank 1
answered on 13 Jan 2010, 05:50 PM
Thanks for the response Atanas.

Unfortunately I ran into a new issue implementing your solution. The code you provided compiles but I get the following exception at runtime:

"Only parameterless constructors and initializers are supported in LINQ to Entities"

This is apparently by design in Entity Framework, if you have any alternate suggestions I'd certainly appreciate it, I'm going to look for an alternate solution as well, if I find one I'll post it here for the community.
0
Atanas Korchev
Telerik team
answered on 14 Jan 2010, 08:52 AM
Hi Justin Bigelow,

Well I didn't see that coming :)

How about exposing a parameterless constructor and mapping the properties? Have you tried this?

var appointmentGridItems = from a in appointmentRepository.ListAppointments()
                           select new AppointmentGridItemViewModel
                           {
                                 Property1 = a.Property1,
                                 Property2 = a.Property2
                           };

I hope this helps,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Justin Bigelow
Top achievements
Rank 1
answered on 14 Jan 2010, 02:18 PM
Hi Atanas,

I had actually tried your suggestion yesterday but I ran into the following error:

The argument to DbIsNullExpression must refer to a primitive or reference type.


Which another community member also ran into (see the thread referenced below.) I was looking for a solution to that before posting again but have so far come up with nothing. I will try and recreate the problem with some generic data and send to to you for review.

http://www.telerik.com/community/forums/aspnet-mvc/grid/the-argument-to-dbisnullexpression-must-refer-to-a-primitive-or-reference-type.aspx

Thanks,
Justin
Tags
Grid
Asked by
Justin Bigelow
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Justin Bigelow
Top achievements
Rank 1
Share this question
or