Hello,
I am developing an AJAX MVC web application with DB first entity model and am really struggling to get entities (and relationships) converted to Json objects to be displayed in a grid.
I have come up with a solution (as described below) which seems to be working, but I am looking for ideas or improvements on this solution.
I have explained the scenario using a simplified example below.
Simple Entity model
Application
ID
CountryID
Code
Name
Country
ID
Code
Name
ApplicationListVM
Contains properties to be displayed in the grid
public class ApplicationListVM
{
public int ID { get; set; }
public string CountryCode { get; set; }
public string CountryName{ get; set; }
public string ApplicationCode { get; set; }
public string ApplicationName { get; set; }
}
My grid declaration (razor)
@(Html.Kendo().Grid<ApplicationListVM>()
.Name("Grid_Ajax")
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("ReadApplicationList", "Application")) // Set the action method which will return the data in JSON format
)
.Columns(columns =>
{
columns.Bound(application => application.ID);
columns.Bound(application => application.CountryCode);
columns.Bound(application => application.CountryName);
columns.Bound(application => application.ApplicationCode);
columns.Bound(application => application.ApplicationName);
})
.Pageable()
.Sortable()
)
ApplicationController
Controller with action method to return data to be displayed in the list
public ActionResult ReadApplicationList([DataSourceRequest]DataSourceRequest request)
{
using (var businessEntities = new Entities())
{
businessEntities.Configuration.LazyLoadingEnabled = false;
var dbApplications = (from application in businessEntities.Applications
select new
{
ID = application.ID,
CountryCode = application.Country.Code,
CountryName = application.Country.Name,
ApplicationCode = application.Code,
ApplicationName = application.Name
}).ToList();
List<ApplicationListVM> applications = new List<ApplicationListVM>();
foreach (var dbApplication in dbApplications)
{
applications.Add(new ApplicationListVM
{
ID = dbApplication.ID,
CountryCode = dbApplication.CountryCode,
CountryName = dbApplication.CountryName,
ApplicationCode = dbApplication.ApplicationCode,
ApplicationName = dbApplication.ApplicationName
});
}
return Json(applications.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
}
This appears to be working and the grid is loading data via AJAX as expected.
I tried loading just the Application entity in the action method, thinking that the related entity (Country) will be automatically loaded and returned in the Json string but the Country property was null!
Therefore, I tried to get around the entity relationships by using a ViewModel which combines the properties from various entities into a single class.
For sure there has to be a better and more efficient way of doing this instead of creating View Models to combine properties from various entities into a single class? How can I improve the solution above?
Thanks in advnace
JB