简体   繁体   English

强类型母版页多态性-嵌套母版页忽略继承属性

[英]Strongly typed master pages polymorphism - nested masterpages ignore inherit attribute

I'm currently creating a CMS system and found that the following doesn't work. 我当前正在创建CMS系统,发现以下内容不起作用。

I do have a work around that isn't exactly ideal and feels dirty. 我确实有一个工作不太理想,感觉很脏。 I'm cool with it for now and not really that interested in a different approach (but don't let that stop you answering). 我现在很酷,对另一种方法并不十分感兴趣(但不要让这种情况阻止您回答)。 What I am after is some kind of explaination on why it doesn't work - is it a bug in ASP.NET MVC? 我所要解决的是为什么它不起作用的某种解释-它是ASP.NET MVC中的错误吗?

It's hard to explain so I'll let my code (minus alot of fluff) do the talking... hope it makes sense! 很难解释,所以我让我的代码(减去大量的绒毛)开始讨论……希望这样做有意义!

EDIT: It seems that the compiler totally ignores the second masterpage's 'inherits' attribute - see at the bottom of the question. 编辑:似乎编译器完全忽略了第二个母版页的'inherits'属性-请参阅问题底部。

ContentViewData.cs - notice it inherits from BaseViewData ContentViewData.cs-注意它是从BaseViewData继承的

public class ContentViewData : BaseViewData
{
    public MyCMS.Data.Models.Content ContentItem { get; set; }
}

Site.Master - Notice the strongly typed viewdata of type BaseViewData Site.Master-注意类型为BaseViewData的强类型视图数据

<%@ Master
    Language="C#"
    Inherits="System.Web.Mvc.ViewMasterPage<MyCMS.WebSite.ViewData.BaseViewData>" %>

Content.Master - Notice the strongly typed viewdata of type ContentViewData and the fact that it's a child masterpage of Site.Master Content.Master-注意类型为ContentViewData的强类型视图数据,并且它是Site.Master的子母版这一事实

<%@ Master
    Language="C#"
    MasterPageFile="~/Views/Shared/Site.Master"
    Inherits="System.Web.Mvc.ViewMasterPage<MyCMS.WebSite.ViewData.ContentViewData>" %>

...blah blah blah...

<% Html.RenderPartial("ContentItemImage", Model.ContentItem); %>

ContentItemImage.ascx ContentItemImage.ascx

<%@ Control
    Language="C#"
    Inherits="System.Web.Mvc.ViewUserControl<MyCMS.Data.Models.Content>" %>

<% if (Model.HasPrimaryPhoto)
   { %>
    <img src="/content/photos/<%= Model.GetPrimaryPhoto.ThumbFileName %>"
         title="<%= Model.GetPrimaryPhoto.Caption %>" />
<% } %>

Now inside the Content.Master if I try and render the ContentItemImage partial and refer to a property on the ContentViewData object (specifically the 'ContentItem' property) like I have - repeated below. 现在在Content.Master内部,如果我尝试渲染ContentItemImage部分并像我一样引用ContentViewData对象的属性(特别是“ ContentItem”属性),请在下面重复。

<% Html.RenderPartial("ContentItemImage", Model.ContentItem); %>

If falls over on that line with the following error 如果跌倒在那条线上,并出现以下错误

Compilation Error 编译错误

CS1061: 'object' does not contain a definition for 'ContentItem' and no extension method 'ContentItem' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) CS1061:“对象”不包含“ ContentItem”的定义,找不到找不到接受类型为“ object”的第一个参数的扩展方法“ ContentItem”(您是否缺少using指令或程序集引用?)

BUT if I change things up like so, it all works fine and dandy. 但是,如果我像这样改变一切,那么一切都很好,很花哨。

Content.Master - Notice I'm passing into RenderPartial() the whole Model (ContentViewData object) rather than trying to refer to a property on the ContentViewData object Content.Master-注意,我将整个模型(ContentViewData对象)传递给RenderPartial(),而不是尝试引用ContentViewData对象上的属性

<%@ Master
    Language="C#"
    MasterPageFile="~/Views/Shared/Site.Master"
    Inherits="System.Web.Mvc.ViewMasterPage<MyCMS.WebSite.ViewData.ContentViewData>" %>

...blah blah blah...

<% Html.RenderPartial("ContentItemImage", Model); %>

ContentItemImage.ascx - notice the changed strongly typed viewdata from MyCMS.Data.Models.Content to the ContentViewData class. ContentItemImage.ascx-注意将MyCMS.Data.Models.Content的强类型化视图数据更改为ContentViewData类。

<%@ Control
    Language="C#"
    Inherits="System.Web.Mvc.ViewUserControl<MyCMS.WebSite.ViewData.ContentViewData>" %>

<% if (Model.ContentItem.HasPrimaryPhoto)
   { %>
    <img src="/content/photos/<%= Model.ContentItem.GetPrimaryPhoto.ThumbFileName %>"
         title="<%= Model.ContentItem.GetPrimaryPhoto.Caption %>" />
<% } %>

So yeah, that works but it aint go not alibi. 是的,那行得通,但不是没有理由。

Thanks in advance, Charles. 预先感谢,查尔斯。

EDIT: Interestingly it seems that the compiler totally ignores the second master page's 'inherits' attribute. 编辑:有趣的是,似乎编译器完全忽略了第二个母版页的'inherits'属性。

Eg. 例如。 I can do this and it still compiles without a complaint... 我可以这样做,它仍然可以毫无抱怨地编译...

<%@ Master
    Language="C#"
    MasterPageFile="~/Views/Shared/Site.Master"
    Inherits="System.Web.Mvc.ViewMasterPage<ThisDoesntExist.AtAll>" %>

Interesting...A resonable guess would be that your ContentViewData object is being upcast to BaseViewData due to some interaction with your nested master page (if that is indeed the case, someone else will need to weigh in as to why). 有趣的...一个合理的猜测是,由于您与嵌套母版页发生了某些交互(因此,您的ContentViewData对象将被转换为BaseViewData(如果确实如此,则其他人将需要权衡其原因))。

You could verify trying this: 您可以验证尝试这样做:

<% Html.RenderPartial("ContentItemImage", ((MyCMS.WebSite.ViewData.ContentViewData)Model).ContentItem); %>

The reason your workaround "works" is because your partial view is typed for ContentViewData, so when you pass in Model it is downcast to that type. 解决方法“起作用”的原因是因为为ContentViewData输入了部分视图,因此当您传递Model时,它会向下转换为该类型。

Just try to do the next thing: 只需尝试做下一件事:

<%@ Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewMasterPage<MyCMS.WebSite.ViewData.ContentViewData>" %>

...blah blah blah...

<%= string.Empty %>

...blah blah blah...

<% Html.RenderPartial("ContentItemImage", Model.ContentItem); %>

Don't ask anything, just try to put <%= string.Empty %> in your master page's code and then use your strongly typed Model property. 不要问任何问题,只需尝试将<%= string.Empty%>放入母版页代码中,然后使用强类型的Model属性。

As you can see from my edit, it seems that the compiler totally ignores the nested master page's 'inherits' attribute. 从编辑中可以看到,似乎编译器完全忽略了嵌套母版页的'inherits'属性。

This leads me to believe that a nested masterpage in ASP.NET MVC will always inherit from it's parent masterpage and as I've witnessed, totally ignore the inherits attribute. 这使我相信,ASP.NET MVC中的嵌套母版将始终从其父母版继承,并且据我所知,完全忽略了继承属性。

EDIT: There must be some magic going on here... If I remove the 'inherits' attribute it won't compile because it doesn't know about the HtmlHelper class. 编辑:这里一定有一些不可思议的事情...如果我删除'inherits'属性,它将无法编译,因为它不了解HtmlHelper类。 But if I have the 'inherits' attribute in there with garbage inside it, it does compile. 但是,如果我在其中具有“ inherits”属性,并且内部有垃圾,它将进行编译。

Doesn't work 不起作用

<%@ Master
    Language="C#"
    MasterPageFile="~/Views/Shared/Site.Master" %>

Does work 确实有效

<%@ Master
    Language="C#"
    MasterPageFile="~/Views/Shared/Site.Master"
    Inherits="Sysasfdaasdtem.Web.Mvsdfc.ViewMasterPage<ThisDoesntExist.AtAll>" %>

Very odd indeed. 确实很奇怪。

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

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