RuntimeBinderException访问鉴于动态匿名类型时, [英] RuntimeBinderException when accessing dynamic anonymous type in view

查看:144
本文介绍了RuntimeBinderException访问鉴于动态匿名类型时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的反常现象,同时学习/用asp.net修修补补。

我试图表明这样的局部视图:

  @ Html.Partial(_ PartialView,新{行动=富})

当我试图与访问操作

  //抛出Microsoft.Csharp.RuntimeBinder.RuntimeBinderException
字符串throwsException = Model.Action;

与消息RuntimeBinderExceptionis


  

对象不包含定义操作


被抛出。结果
奇怪的是,该行正常工作:

  //这行工作正常
字符串工作=((类型)Model.GetType())的getProperty(行动)的GetValue(型号)。

此行​​为使我迷惑不解颇有几分,我宁愿避免使用此解决办法。此外,我不认为这个问题是<一个href=\"http://stackoverflow.com/questions/5120317/dynamic-anonymous-type-in-razor-causes-runtimebinderexception\">anonymous类型是内部因为ASP.NET项目在VS2013 MVC的模板,这是否成功:

在这里发生了什么?


解决方案

  

在这里发生了什么?


您的局部视图是弱类型。你没有一个 @model 定义它。所以默认情况下它是对象这显然不具有动作属性。

要解决这个问题的正确方法是定义视图模型:

 公共类MyViewModel
{
    公共字符串操作{搞定;组; }
}

这是你的部分观点将是强类型为:

  @model MyViewModel
@ {
    字符串throwsException = Model.Action;
}

和将由主视图进行传递:

  @ Html.Partial(_ PartialView,新MyViewModel {行动=富})

另一种可能性(这我个人不喜欢,因为它依赖于运行时绑定)被使用在局部视图动态模型:

  @model动态
@ {
    字符串throwsException = Model.Action;
}

,然后调用它时,你将能够通过一个匿名对象:

  @ Html.Partial(_ PartialView,新{行动=富})

I've encountered a strange anomaly while learning/tinkering with asp.net.

I'm trying to show a partial view like this:

@Html.Partial("_PartialView", new { Action = "Foo" })

When I'm trying to access Action with

// Throws Microsoft.Csharp.RuntimeBinder.RuntimeBinderException
string throwsException = Model.Action; 

a RuntimeBinderExceptionis with the message

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

is thrown.
The strange thing is that this line works fine:

// This line works fine
string works = ((Type)Model.GetType()).GetProperty("Action").GetValue(Model);

This behavior puzzles me quite a bit and I'd rather avoid using this workaround. Also I don't think the problem is anonymous types being internal because the MVC template for ASP.NET Project in VS2013 does this successfully:

So what happened here?

解决方案

So what happened here?

Your partial view is weakly typed. You do not have a @model definition for it. So by default it is object which obviously doesn't have an Action property.

The correct way to solve this is to define a view model:

public class MyViewModel
{
    public string Action { get; set; }
}

that your partial view will be strongly typed to:

@model MyViewModel
@{
    string throwsException = Model.Action; 
}

and which will be passed by the main view:

@Html.Partial("_PartialView", new MyViewModel { Action = "Foo" })

Another possibility (which personally I don't like as it relies on runtime binding) is to use a dynamic model in the partial view:

@model dynamic
@{
    string throwsException = Model.Action; 
}

and then you will be able to pass an anonymous object when calling it:

@Html.Partial("_PartialView", new { Action = "Foo" })

这篇关于RuntimeBinderException访问鉴于动态匿名类型时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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