DataSourceResult map to ViewModel in Controller. How To

2 posts, 0 answers
  1. Zac Everett
    Zac Everett avatar
    16 posts
    Member since:
    Sep 2012

    Posted 24 Sep 2013 Link to this post

    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?






  2. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 26 Sep 2013 Link to this post

    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!
Back to Top