简体   繁体   English

在嵌套在子文件夹中的Partial中使用@model dynamic会引发RuntimeBinderException

[英]Using @model dynamic in a Partial nested in a subfolder throws RuntimeBinderException

I'm having some problems finding a solution for this, though it seems straight-forward enough, and I imagine someone else must have run into this issue before. 尽管看起来很简单,但我在寻找解决方案时遇到了一些问题,并且我想其他人以前一定也遇到过此问题。

Using MVC/Razor 4, I am trying to render a partial using a dynamic model. 使用MVC / Razor 4,我尝试使用动态模型渲染局部。 To organize things, I have placed my partials in a sub-folder within the same view folder. 为了组织事情,我将局部文件放在了同一视图文件夹中的子文件夹中。

When the partial in question is moved to the sub-folder, it throws a RuntimeBinderException with an exception message saying that 'object' does not contain a definition for 'Id' (the parameter I am trying to access). 当有问题的部分移至子文件夹时,它将引发RuntimeBinderException异常消息,提示“对象”不包含“ Id”(我正在尝试访问的参数)的定义。

This works perfectly fine when the partial is located in the same folder. 当局部文件位于同一文件夹中时,这工作得很好。

This structure works fine 这个结构工作正常

  • Views/Orders/Details.cshtml 查看/订单/ Details.cshtml
  • Views/Orders/_PartialWithDynamicModel.cshtml 查看/订单/ _PartialWithDynamicModel.cshtml

This structure causes the exception 此结构导致异常

  • Views/Orders/Details.cshtml 查看/订单/ Details.cshtml
  • Views/Orders/MyPartials/_PartialWithDynamicModel.cshtml 查看/订单/ MyPartials / _PartialWithDynamicModel.cshtml

CODE

Details.cshtml Details.cshtml

    @Html.Partial("MyPartials/_PartialWithDynamicModel", new { Id = 54 } )

_PartialWithDynamicModel.cshtml _PartialWithDynamicModel.cshtml

    @model dynamic

    @ { //The following line throws the RuntimeBinderException
        int id = Model.Id; }

Any thoughts? 有什么想法吗? If I move the partial into the same folder as the view, everything works fine. 如果我将局部视图移到与视图相同的文件夹中,则一切正常。

Your problem is that you can't pass an anonymous type to an object in a separate assembly. 您的问题是您不能将匿名类型传递给单独程序集中的对象。 They are created as "internal" types, and thus cannot be passed externally. 它们被创建为“内部”类型,因此无法从外部传递。 Views are generated dynamically at runtime into their own assemblies. 视图在运行时动态生成到它们自己的程序集中。

Instead, use an ExpandoObject, like this: 而是使用ExpandoObject,如下所示:

@{ var myExpando = new ExpandoObject();
   myExpando.Id = 54; }
@Html.Partial("MyPartials/_PutOnHoldForm",  myExpando)

A better choice, however, would be to just pass a ViewDataDictionary, or perhaps use Tuples. 但是,更好的选择是仅传递ViewDataDictionary,或者使用Tuples。

There is also the DynamicViewPage extension in the MVC futures project that allows you to do this as well without the expand object. MVC期货项目中还提供了DynamicViewPage扩展,您无需扩展对象也可以执行此操作。

http://weblogs.asp.net/imranbaloch/using-the-features-of-asp-net-mvc-3-futures http://weblogs.asp.net/imranbaloch/using-the-features-of-asp-net-mvc-3-futures

(note, it says MVC3, but there is an MVC5 version of futures in Nuget) (注意,它说的是MVC3,但Nuget中有期货的MVC5版本)

暂无
暂无

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

相关问题 事件回调<dynamic>抛出 RuntimeBinderException</dynamic> - EventCallback<dynamic> throws RuntimeBinderException 动态属性分配引发RuntimeBinderException - Dynamic Property Assignment Throws RuntimeBinderException 动态转换为ObjectHandle会引发RuntimeBinderException - Casting dynamic to ObjectHandle throws RuntimeBinderException 为什么“((dynamic)dictionary).MyKey”抛出RuntimeBinderException? - Why “((dynamic) dictionary).MyKey” throws a RuntimeBinderException? 使用动态对象时的RuntimeBinderException - RuntimeBinderException when using dynamic object 将C#动态与COM对象一起使用会引发RuntimeBinderException,以记录已实现接口的方法 - Using C# dynamic with COM object throws RuntimeBinderException for documented method of implemented interface 为什么“动态”ExpandoObject 即使包含属性定义也会抛出 RuntimeBinderException? - Why 'dynamic' ExpandoObject throws RuntimeBinderException even if it contains the definition for a property? C#动态-“ RuntimeBinderException” - C# Dynamic - “RuntimeBinderException” 安装Reactive Extensions后使用Newtonsoft Json和动态时的RuntimeBinderException - RuntimeBinderException while using Newtonsoft Json with dynamic after installing Reactive Extensions 从继承的接口调用Method时传递动态参数会引发RuntimeBinderException - Passing a dynamic parameter throws RuntimeBinderException when calling Method from Inherited interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM