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

DataSourceResult map to ViewModel in Controller. How To

1 Answer 1004 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Zac Everett
Top achievements
Rank 1
Zac Everett asked on 24 Sep 2013, 02:54 PM
I would like to be able to map a DataSourceResult to a ViewModel in my controller class, is this possible?

I have a pretty normal MVC application with a ViewModel
public class UserViewModel
   {
       public UserViewModel()
       {
           WebSiteUsers = new List<WebSiteUser>();
       }
       public List<WebSiteUser> WebSiteUsers { get; set; }
   }
 
   public class WebSiteUser
   {
       public int UserId { get; set; }
       public string Username { get; set; }
       public string FirstName { get; set; }
       public string LastName { get; set; }
   }

And a normal Grid in razor
@(Html.Kendo().Grid<Views.Users.ViewModel.WebSiteUser>()
    .Name("Grid")
    .Columns(columns =>
        {
            columns.Bound(p => p.UserId);
            columns.Bound(p => p.Username);
            columns.Bound(p => p.FirstName);
            columns.Bound(p => p.LastName);
        })
         
        .Pageable(p => p.PageSizes(true))
     .HtmlAttributes(new { style = "height:500px;" })
     .DataSource(dataSource => dataSource
         .Ajax()
         .PageSize(20)
         .Model(model => model.Id(p => p.UserId))
         .Read(read => read.Action("Users_Read", "Users"))
     )
 
)
My controller has the following action for AJAX binding for filtering.

public ActionResult Users_Read([DataSourceRequest]DataSourceRequest request)
       {
           DataSourceResult result = _user.GetSiteUsersKendo(request);
 
           return Json(result);
       }

My service class has the method for getting the users.
public DataSourceResult GetSiteUsersKendo([DataSourceRequest]DataSourceRequest request)
       {
           using (TestEntities context = new Models.TestEntities())
           {
               IQueryable<Models.WebSiteUser> webSiteUsers = context.WebSiteUsers;
 
               DataSourceResult result = webSiteUsers.ToDataSourceResult(request);
 
               return result;
           }
       }

This code returns a Javascripts serialisaztion error - Object connection closed. I presume this is because the JSON call in the controller is trying to serialise the entity framework object perhaps trying to perform some kind of lazy loading but of course the connection to the DB is closed as it's in a 'Using' statement.

What I'm trying to achieve is a way to return a ViewModel to the grid from the controller action. 

I can force the DataSourceResult to map to a POCO using the following code in my service class. I do NOT want to use a ViewModel in my service class, only the controllers should be aware of the ViewModels.
DataSourceResult result = webSiteUsers.ToDataSourceResult(request, su => new Models.UserModels.WebSiteUser{
                    UserId = su.UserId,
                    Username = su.Username
                });
But then I still need to map the DataSourceResult to a ViewModel in the controller.

Is this possible?






1 Answer, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 26 Sep 2013, 03:01 PM
Hello Zac,

The only thing I can think of as a work-around is to pass that mapping lamba function as an argument to the service method and use it inside.

Kind Regards,
Petur Subev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Zac Everett
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Share this question
or