动态类型在mvc视图中 [英] dynamic type in mvc view

查看:97
本文介绍了动态类型在mvc视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Razor中的动态匿名类型导致RuntimeBinderException


我试图在我的MVC应用程序中使用动态类型模型。我有以下代码:
in controller:

  var model = new {Name =test name,Family =m}; 
return this.View(model);

在我看到的视图中:

  @model dynamic 

@if(Model!= null)
{
< p> @ Html.Raw(Model.Name)< / p>
}

当我运行这个,我得到以下错误:

 'object'不包含'Name'的定义(System.Exception {Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)

为什么会收到此错误?
在调试期间,如果我将光标放在@Model上,我可以看到它有两个名为Name和Family的属性。

解决方案

您所显示的不是动态类型。这是一个匿名的类型。有很大的区别。



您不能使用匿名类型作为模型。原因是因为编译器将匿名类型发送为内部。这意味着它们只能通过当前程序集访问。但是,您知道,Razor视图由ASP.NET运行时编译为单独的程序集,无法使用这些匿名类型。



在这种情况下,正确的方法是使用视图模型:

  public class MyViewModel 
{
public string Name {get;组; }
public string Family {get;组;
}

然后让您的控制器操作将此视图模型传递给视图:新新p新新新新旗新新新旗新新旗新新新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新款:
return this.View(model);

,以便您的视图可以使用:

  @model MyViewModel 
@if(Model!= null)
{
< p> @ Model.Name< / p>
}

有些人(不是我,我绝对不会推荐任何这样的东西)也使用 ViewBag ,这样他们就不需要一个模型:

  ViewBag .Name =test name; 
ViewBag.Family =m;
return this.View();

然后在视图中:

 < p为H. @ ViewBag.Name< / p为H. 


Possible Duplicate:
Dynamic Anonymous type in Razor causes RuntimeBinderException

I am trying to use a dynamic type model in my MVC application. I have the following code: in controller:

var model = new { Name = "test name", Family = "m" };
return this.View(model);

and in the view I have:

@model dynamic

@if(Model!=null)
{
   <p> @Html.Raw(Model.Name) </p>
}

When I am running this, I am getting the following error :

'object' does not contain a definition for 'Name'   (System.Exception {Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)

Why do I get this error? During debug, if I put my cursor on @Model, I can see that it has two property called Name and Family.

解决方案

What you have shown is not a dynamic type. It's an anonymous type. There's a huge difference.

You cannot use anonymous types as models. The reason for this is because the compiler emits anonymous types as internal. This means that they are only accessible withing the current assembly. But as you know Razor views are compiled by the ASP.NET runtime as separate assemblies which have no way of using those anonymous types.

Obviously the correct way in this case is to use a view model:

public class MyViewModel
{
    public string Name { get; set; }
    public string Family { get; set; }
}

and then have your controller action pass this view model to the view:

var model = new MyViewModel { Name = "test name", Family = "m" };
return this.View(model);

so that your view can work with it:

@model MyViewModel
@if (Model != null)
{
    <p>@Model.Name</p>
}

Some people (not me, I would never recommend anything like this) also use ViewBag and this way they don't need a model:

ViewBag.Name = "test name";
ViewBag.Family = "m";
return this.View();

and then in the view:

<p>@ViewBag.Name</p>

这篇关于动态类型在mvc视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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