Convert lambda expression to Json

懵懂的女人 提交于 2019-12-02 09:08:16

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:

public ActionResult Index_Read([DataSourceRequest] DataSourceRequest request)
    {
        var dataContext = repository.Students;
        var students = dataContext.ToDataSourceResult(request, m => new StudentViewModel
        {
            ID = m.ID,
            Course = m.Course,
            CityName = m.City.Name, 
            RegionName = m.City.Region.Name 
        });           
        return Json(students, JsonRequestBehavior.AllowGet);
    }

public class StudentViewModel
{
    public int ID { get; set; }
    public string Course { get; set; }
    public string CityName { get; set; }
    public string RegionName { get; set; }
}

In View:

@model IEnumerable<StudentViewModel>

@(Html.Kendo().Grid<StudentViewModel>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(m => m.ID);
        columns.Bound(m => m.Course);
        columns.Bound(m => m.CityName);
        columns.Bound(m => m.RegionName);
    })
    .Pageable()
    .Sortable()
    .Filterable()
    .Scrollable()
    .Groupable()    
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("Index_Read", "Student"))
    )        
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!