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

Grid + Ajax + Entity Framework + Json

1 Answer 198 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jyot Boparai
Top achievements
Rank 1
Jyot Boparai asked on 22 May 2013, 04:19 AM
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

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 23 May 2013, 06:22 AM
Hello,

 Using view models to serialize entities to JSON is considered a good practice. The ToDataSourceResult action method supports an overload which can be used to map entities to view models. More info can be found in the ajax binding help topic..

Regards,
Atanas Korchev
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
Jyot Boparai
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or