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
And a normal Grid in razor
My controller has the following action for AJAX binding for filtering.
My service class has the method for getting the users.
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.
But then I still need to map the DataSourceResult to a ViewModel in the controller.
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"))
)
)
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
});
Is this possible?