简体   繁体   English

RuntimeBinderException 与 MVC 中的动态匿名对象

[英]RuntimeBinderException with dynamic anonymous objects in MVC

The code编码

I've got an MVC project with a partial page that looks somewhat like this:我有一个 MVC 项目,其部分页面看起来像这样:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<div class="tab-window <%= Model.TargetClass %> <%= Model.TargetTab == Model.SelectedTab ? "selected" : "" %>"
    data-window-url="/SomeUrl/Partial/<%= Model.TargetTab %>/"
    <%= Model.TargetTab == Model.SelectedTab ? "data-content-loaded=\"true\"" : "" %>>
    <% if (Model.TargetTab == Model.SelectedTab) {
           Html.RenderPartial(Model.TargetTab as string, Model.Model as object);
        } %>
</div>

What it does is open another partial (the one named in Model.TargetTab ) with Model.Model if it's the currently visible tab, otherwise just renders an empty div (which is loaded with jQuery when needed). What it does is open another partial (the one named in Model.TargetTab ) with Model.Model if it's the currently visible tab, otherwise just renders an empty div (which is loaded with jQuery when needed).

It's called like this:它是这样称呼的:

<% Html.RenderPartial("TabWindowContainer", new { TargetTab = "MyTabName", TargetClass = "my-tab-class", SelectedTab = Model.Tab, Model = Model }); %>

This used to work.这曾经奏效。

Then I changed the value that goes into the Model , and it stopped working.然后我更改了进入Model的值,它停止工作。 I changed it back, and it's still not working.我改回来了,还是不行。 To be clear, hg status currently doesn't show any of these files.需要明确的是, hg status 目前不显示任何这些文件。

The exception例外

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'TargetClass'

When you try to open Model in the Quickwatch window you see it has all the properties setup with correct values当您尝试在 Quickwatch window 中打开Model时,您会看到它具有正确值的所有属性设置

快速手表

But when you try to view any property, you get the same exception as before但是当您尝试查看任何属性时,您会遇到与以前相同的异常

快速观看异常

Thinking about it, I realized that possibly, this shouldn't work at all; 想了想,我意识到可能,这根本不应该工作; the object we're trying to access is from a different assembly so we wouldn't see its properties. 我们尝试访问的 object 来自不同的程序集,因此我们看不到它的属性。 But then, why did it use to work? 但是,为什么它曾经工作呢? I still have a running version where this works. 我仍然有一个可以运行的版本。 What can I do to make it work again? 我该怎么做才能让它再次工作?

Update: It should work;更新:它应该工作; the model is coming from another view in the same assembly, not from the controller. model 来自同一组件中的另一个视图,而不是来自 controller。

Your dynamic type can't find the properties within the anonymous type because the anonymous type's properties are internal (not public).您的动态类型在匿名类型中找不到属性,因为匿名类型的属性是内部的(不是公共的)。 Thus, your app throws an exception even thought the anonymous type's properties are plainly visible to the debugger.因此,即使匿名类型的属性对调试器可见,您的应用也会引发异常。 Reference.参考。

Create an Expando extension method.创建一个 Expando 扩展方法。

public static ExpandoObject ToExpando(this object anonymousObject)
            {
                IDictionary<string, object> anonymousDictionary = new RouteValueDictionary(anonymousObject);
                IDictionary<string, object> expando = new ExpandoObject();
                foreach (var item in anonymousDictionary)
                    expando.Add(item);
                return (ExpandoObject)expando;
            }

Apply the extension as so.像这样应用扩展。

<% Html.RenderPartial("TabWindowContainer", new { TargetTab = "MyTabName", TargetClass = "my-tab-class", SelectedTab = Model.Tab, Model = Model }.ToExpando()); %>

Hopefully, this will work and I didn't embarrass myself by misunderstanding the issue.希望这会奏效,我并没有因为误解这个问题而让自己难堪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM