'对象'不包含'X'的定义 [英] 'object' does not contain a definition for 'X'

查看:83
本文介绍了'对象'不包含'X'的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经收到了这个问题并没有解决。我有一个列表(在MVC3控制器生成):

  ViewBag.Languages​​ = db.Languages
    。选择(X =>新建{x.Name,x.EnglishName,x.Id})
    .ToList();

和我的网页(剃刀)在我试图来遍历它:

 的foreach(在ViewBag.Languages​​变种O)
{
    字符串IMG =郎/+ o.EnglishName +巴纽;
    @ *工作* @
}

但参照 o.EnglishName 失败,出现错误:


  

对象不包含定义为中文名


虽然奇怪的是,如果我键入到立即窗口(同时调试):


  0
{名称=བོད་སྐད་,中文名藏族=,ID = 31}
    中文名:藏
    编号:31
    产品名称:བོད་སྐད་


如此明显的领域是存在的。这里有什么我的问题?


解决方案

您在这里使用一个匿名对象:

  ViewBag.Languages​​ = db.Languages
    。选择(X =>新建{x.Name,x.EnglishName,x.Id})
    .ToList();

匿名对象由编译器发出的内部。剃刀视图将自动编译到由ASP.NET运行一个单独的程序。这意味着你无法访问您的控制器产生的任何匿名对象。

因此​​,为了解决您的问题,你可以定义视图模型:

 公共类LanguageViewModel
{
    公众诠释标识{搞定;组; }
    公共字符串名称{;组; }
    公共字符串中文名{获得;组; }
}

,然后在您的控制器使用该视图模型:

  ViewBag.Languages​​ = db.Languages
    。选择(X =>新建LanguageViewModel
    {
        名称= x.Name,
        中文名= x.EnglishName,
        ID = x.Id
     })
    .ToList();

而现在,你有一个视图模型下次改进到code是当然要摆脱这种废话 ViewBag 中的我有病看到的并且只需使用视图模型和强类型:

 公众的ActionResult美孚()
{
    VAR模型=分贝
        .Languages
        。选择(X =>新建LanguageViewModel
        {
            名称= x.Name,
            中文名= x.EnglishName,
            ID = x.Id
        })
        .ToList();
    返回查看(模型);
}

和然后当然有一个强类型的视图:

  @model IEnumerable的< LanguageViewModel>
@ Html.DisplayForModel()

,然后定义它会自动通过ASP.NET MVC引擎视图模型中的每个元素呈现相应的显示模板,让你甚至都不需要编写一个单一的foreach在你的意见(〜/查看/共享/ DisplayTemplates / LanguageViewModel.cshtml

  @model LanguageViewModel
...生成的图像或任何你试图在第一个地方做

I had this problem once before and didn't resolve it. I have a list (generated in an MVC3 controller):

ViewBag.Languages = db.Languages
    .Select(x => new { x.Name, x.EnglishName, x.Id })
    .ToList();

and on my page (Razor) I try to iterate through it:

foreach (var o in ViewBag.Languages)
{
    string img = "Lang/" + o.EnglishName + ".png";
    @* work *@
}

but the reference to o.EnglishName fails with the error:

'object' does not contain a definition for 'EnglishName'

though the curious thing is that if I type into the Immediate Window (whilst debugging):

o
{ Name = བོད་སྐད་, EnglishName = Tibetan, Id = 31 }
    EnglishName: "Tibetan"
    Id: 31
    Name: "བོད་སྐད་"

so obviously the field is there. What is my problem here?

解决方案

You are using an anonymous object here:

ViewBag.Languages = db.Languages
    .Select(x => new { x.Name, x.EnglishName, x.Id })
    .ToList();

Anonymous objects are emitted as internal by the compiler. The Razor views are automatically compiled into a separate assembly by the ASP.NET runtime. This means that you cannot access any anonymous objects generated in your controllers.

So in order to fix your issue you could define a view model:

public class LanguageViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string EnglishName { get; set; }
}

and then in your controller use this view model:

ViewBag.Languages = db.Languages
    .Select(x => new LanguageViewModel
    { 
        Name = x.Name, 
        EnglishName = x.EnglishName, 
        Id = x.Id 
     })
    .ToList();

And now that you have a view model the next improvement to your code is of course to get rid of this crap of ViewBag that I am sick of seeing and simply use view models and strong typing:

public ActionResult Foo()
{
    var model = db
        .Languages
        .Select(x => new LanguageViewModel
        { 
            Name = x.Name, 
            EnglishName = x.EnglishName, 
            Id = x.Id 
        })
        .ToList();
    return View(model);
}

and then of course have a strongly typed view:

@model IEnumerable<LanguageViewModel>
@Html.DisplayForModel()

and then define the corresponding display template which will automatically be rendered by the ASP.NET MVC engine for each element of the view model so that you don't even need to write a single foreach in your views (~/Views/Shared/DisplayTemplates/LanguageViewModel.cshtml):

@model LanguageViewModel
... generate the image or whatever you was attempting to do in the first place

这篇关于'对象'不包含'X'的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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