转换的lambda前pression到JSON在MVC [英] Convert lambda expression to Json in MVC

查看:159
本文介绍了转换的lambda前pression到JSON在MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个错误(无法将拉姆达前pression键入字符串',因为它不是一个委托类型)的控制器转换拉姆达前pression期间。我有3个实体如下:

实体:

 公共类学生
{
    公众诠释ID {搞定;组; }    公共字符串课程{搞定;组; }    公众诠释CityID {搞定;组; }    公共虚拟城市城市{搞定;组; }
}
公共类城市
{
    公众诠释ID {搞定;组; }    公共字符串名称{;组; }    公众诠释RegionID {搞定;组; }    公共虚拟地区地区{搞定;组; }    公共虚拟的ICollection<的Student GT;学生{搞定;组; }
}
公共类地区
{
    公众诠释ID {搞定;组; }    公共字符串名称{;组; }    公共虚拟的ICollection<城市>城市{搞定;组; }
}



控制器:

 公众的ActionResult Index_Read([DataSourceRequest] DataSourceRequest要求)
    {
        VAR的DataContext = repository.Students;
        VAR学生= dataContext.ToDataSourceResult(请求,M =>新建
        {
            ID = m.ID,
            当然= m.Course,            市= m.City.Name,//我能得到城市名称,并显示在视图。
            MyRegionName = m.City.Region.Name //我可以得到域名,并将其分配给
//MyRegionName参数JSON。然而,在查看我可以用MyRegionNameparemeter不明白
        });        返回JSON(学生,JsonRequestBehavior.AllowGet);
    }



查看:

  @model IEnumerable的<的Student GT;
@(Html.Kendo()网格和LT;学生>()
    。名称(网格)
    .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(数据源=>数据源
        阿贾克斯()
        .Read(读=> read.Action(Index_Read,学生))
    )

下面是可能会导致控制器和视图的问题点:

  =城m.City.Name,//我能得到城市名称,并显示在视图。
MyRegionName = m.City.Region.Name //我可以得到区域名称,并将其分配给JSON的MyRegionName参数。然而,在查看我可以用MyRegionNameparemeter明白这一点。

或许它关系到学生的实体,有城市参数。但是,在城市实体没有 MyRegionName 属性。


解决方案

我想这是因为没有所谓的财产 MyRegionName 学生类。

您有两种选择

1)创建一个视图模型看起来像你的学生的模式,而是使其具有这样的属性。另外,还要确保的投影功能在 ToDataSourceResult 创建这些新视图模型类型而不是匿名对象。

2),只要用一个模板列。例如

<$p$p><$c$c>columns.Template(@<text></text>).Title(\"MyRegionName\").ClientTemplate(\"#=MyRegionName#\");

I get an error ("Cannot convert lambda expression to type 'string' because it is not a delegate type") during converting the lambda expression in controller. I have 3 entities in 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.

解决方案

I assume this happens because there is no property called MyRegionName for the Student class.

You have two options

1) Create a ViewModel that looks like your Student model but make it has such property. Also make sure inside the projection function of the ToDataSourceResult you create those new ViewModel types instead of anonymous object.

2) Just use a Template column. e.g.

columns.Template(@<text></text>).Title("MyRegionName").ClientTemplate("#=MyRegionName#");

这篇关于转换的lambda前pression到JSON在MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆