Razor 中的动态匿名类型导致 RuntimeBinderException
我收到以下错误:
“object”不包含“RatingName”的定义
当您查看匿名动态类型时,它显然具有RatingName。
我意识到我可以使用元组来完成此操作,但我想了解为什么会出现错误消息。
I'm getting the following error:
'object' does not contain a definition for 'RatingName'
When you look at the anonymous dynamic type, it clearly does have RatingName.
I realize I can do this with a Tuple, but I would like to understand why the error message occurs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
在我看来,具有内部属性的匿名类型是一个糟糕的 .NET 框架设计决策。
这里有一个快速且很好的扩展来解决这个问题,即立即将匿名对象转换为 ExpandoObject。
使用起来非常简单:
当然在您看来:
Anonymous types having internal properties is a poor .NET framework design decision, in my opinion.
Here is a quick and nice extension to fix this problem i.e. by converting the anonymous object into an ExpandoObject right away.
It's very easy to use:
Of course in your view:
我在相关问题。答案在 David Ebbo 的博客文章 将匿名对象传递到 MVC 视图并使用动态访问它们
I found the answer in a related question. The answer is specified on David Ebbo's blog post Passing anonymous objects to MVC views and accessing them using dynamic
使用ToExpando方法是最好的解决方案。
以下是不需要 System.Web 程序集的版本:
Using ToExpando method is the best solution.
Here is the version that doesn't require System.Web assembly:
不必像这样从匿名类型创建模型,然后尝试将匿名对象转换为
ExpandoObject
...您可以直接创建
ExpandoObject
:然后在您的view 将模型类型设置为动态 @modeldynamic 并且可以直接访问属性:
我通常建议大多数视图使用强类型视图模型,但有时这种灵活性很方便。
Instead of creating a model from an anonymous type and then trying to convert the anonymous object to an
ExpandoObject
like this ...You can just create the
ExpandoObject
directly:Then in your view you set the model type as dynamic
@model dynamic
and you can access the properties directly :I'd normally recommend strongly typed view models for most views, but sometimes this flexibility is handy.
您可以使用框架impromptu 接口将匿名类型包装在接口中。
您只需返回一个
IEnumerable
并在 Linq 末尾使用.AllActLike();
这可以工作,因为它使用具有声明匿名类型的程序集上下文的 DLR。You can use the framework impromptu interface to wrap an anonymous type in an interface.
You'd just return an
IEnumerable<IMadeUpInterface>
and at the end of your Linq use.AllActLike<IMadeUpInterface>();
this works because it calls the anonymous property using the DLR with a context of the assembly that declared the anonymous type.编写一个控制台应用程序并添加 Mono.Cecil 作为参考(您现在可以从 NuGet 添加它),然后编写以下代码代码:
上面的代码将从输入参数获取汇编文件,并使用 Mono.Cecil 将可访问性从内部更改为公共,这将解决问题。
我们可以在网站的 Post Build 事件中运行该程序。我写了一篇关于此的中文博客文章 但我相信你可以只阅读代码和快照。 :)
Wrote a console application and add Mono.Cecil as reference (you can now add it from NuGet), then write the piece of code:
The code above would get the assembly file from input args and use Mono.Cecil to change the accessibility from internal to public, and that would resolve the problem.
We can run the program in the Post Build event of the website. I wrote a blog post about this in Chinese but I believe you can just read the code and snapshots. :)
根据已接受的答案,我已在控制器中进行了重写,使其在一般情况下和在幕后工作。
代码如下:
现在您只需传递一个匿名对象作为模型,它就会按预期工作。
Based on the accepted answer, I have overridden in the controller to make it work in general and behind the scenes.
Here is the code:
Now you can just pass an anonymous object as the model, and it will work as expected.
我将从 https://stackoverflow.com/a/7478600/37055 偷一些东西
如果你 install-package dynamitey 你可以这样做:
农民们欢欣鼓舞。
I'm going to do a little bit of stealing from https://stackoverflow.com/a/7478600/37055
If you install-package dynamitey you can do this:
And the peasants rejoice.
触发 RuntimeBinderException 的原因,我认为其他帖子中有很好的答案。我只是专注于解释我实际上是如何让它发挥作用的。
通过参考答案@DotNetWise和在ASP.NET MVC中用匿名类型集合绑定视图,
首先,创建一个用于扩展的静态类,
在控制器
View中,@model IEnumerable(动态的,不是模型类),这个很重要,因为我们将绑定匿名类型对象。
foreach 中的类型,我使用 var 或 dynamic 都没有错误。
顺便说一句,创建一个与新字段匹配的新 ViewModel 也可以是将结果传递给视图的方法。
The reason of RuntimeBinderException triggered, I think there have good answer in other posts. I just focus to explain how I actually make it work.
By refer to answer @DotNetWise and Binding views with Anonymous type collection in ASP.NET MVC,
Firstly, Create a static class for extension
In controller
In View, @model IEnumerable (dynamic, not a model class), this is very important as we are going to bind the anonymous type object.
The type in foreach, I have no error either using var or dynamic.
By the way, create a new ViewModel that is matching the new fields also can be the way to pass the result to the view.
现在是递归风格
Now in recursive flavor
使用 ExpandoObject 扩展可以工作,但在使用嵌套匿名对象时会中断。
例如
为了完成这个我使用这个。
控制器中的用法是相同的,只是您使用 ToRazorDynamic() 而不是 ToExpando()。
在您看来,要获取整个匿名对象,您只需在末尾添加“.AnonValue”即可。
Using the ExpandoObject Extension works but breaks when using nested anonymous objects.
Such as
To accomplish this I use this.
Usage in the controller is the same except you use ToRazorDynamic() instead of ToExpando().
In your view to get the entire anonymous object you just add ".AnonValue" to the end.
我尝试了 ExpandoObject,但它不适用于像这样的嵌套匿名复杂类型:
所以我的解决方案是将 JObject 返回到视图模型:
并在 .cshtml 中转换为动态:
I tried the ExpandoObject but it didn't work with a nested anonymous complex type like this:
So my solution was to return a JObject to View model:
and convert to dynamic in .cshtml: