First of all please be sure that none of the solution on stackoverflow has not solved my problem (maybe it is caused from Entity Framework 6). I have 3 entities: Student, City and Region as below:
Entities:
public class Student
{
public int ID { get; set; }
public string Course { get; set; }
public int CityID { get; set; }
public virtual City City { get; set; }
}
public class City
{
public int ID { get; set; }
public string Name { get; set; }
public int RegionID { get; set; }
public virtual Region Region { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
public class Region
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<City> Cities { get; set; }
}
Controller:
public ActionResult Index_Read([DataSourceRequest] DataSourceRequest request)
{
var dataContext = repository.Students;
var students = dataContext.ToDataSourceResult(request, m => new
{
ID = m.ID,
Course = m.Course,
City = m.City.Name, //I can get City name and show it in View.
MyRegionName = m.City.Region.Name //I can get region name and assign it to
//"MyRegionName" parameter in JSON. However in View I cannot get it using "MyRegionName" paremeter
});
return Json(students, JsonRequestBehavior.AllowGet);
}
View:
@model IEnumerable<Student>
@(Html.Kendo().Grid<Student>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(m => m.ID);
columns.Bound(m => m.Course);
columns.Bound(m => m.City);
columns.Bound(m => m.MyRegionName);
})
.Pageable()
.Sortable()
.Filterable()
.Scrollable()
.Groupable()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Index_Read", "Student"))
)
)
Here is the point that may cause the problem in the Controller and View:
City = m.City.Name, //I can get City name and show it in View.
MyRegionName = m.City.Region.Name //I can get region name and assign it to the "MyRegionName" parameter in JSON. However in View I cannot get it using "MyRegionName" paremeter.
May it be related to that there is City
parameter in the Student entity. But there is no MyRegionName
property in the City
entity.
In "Index_Read" method you are creating "IEnumerable of object" i.e. students which is not of "IEnumerable of Student" type. But in view you have binded your grid to "IEnumerable of Student". Since "Student" class doesn't contain "MyRegionName" property that's why you are facing issue.
Try something like this:
In View: