将 lambda 表达式转换为 Json

Convert lambda expression to Json

首先请确定Whosebug上的解决方案none没有解决我的问题(可能是Entity Framework6引起的)。我有 3 个实体:学生、城市和地区,如下所示:

实体:

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; } 
}


控制器:

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);
    }


查看:

@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"))
    )        
)

这里是可能导致Controller和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.

可能与Student实体中有City参数有关。但是City实体中没有MyRegionName属性。

在 "Index_Read" 方法中,您正在创建 "IEnumerable of object" 即非 "IEnumerable of Student" 类型的学生。但鉴于您已将网格绑定到 "IEnumerable of Student"。 由于 "Student" class 不包含 "MyRegionName" 属性 这就是您遇到问题的原因。

尝试这样的事情:

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; }
}

在视图中:

@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"))
    )        
)