将Lambda表达式转换为Json

将Lambda表达式转换为Json

本文介绍了将Lambda表达式转换为Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,请确保所有关于stackoverflow的解决方案都没有解决我的问题(可能是由Entity Framework 6引起的).我有3个实体:学生,城市和地区,如下所示:

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:

实体:

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中出现问题的点:

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.

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

May it be related to that there is City parameter in the Student entity. But there is no MyRegionName property in the City entity.

推荐答案

在"Index_Read"方法中,您将创建"IEnumerable of object"(即对象的IEnumerable),即不是"IEnumerable of Student"类型的学生.但鉴于您已将网格绑定到"IEnumerable of Student".由于学生"类不包含"MyRegionName"属性,这就是您面临问题的原因.

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.

尝试这样的事情:

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

这篇关于将Lambda表达式转换为Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:13