本文介绍了强类型母版页多态性-嵌套母版页忽略继承属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在创建CMS系统,发现以下内容不起作用.

I'm currently creating a CMS system and found that the following doesn't work.

我确实有一个解决方法,它并不十分理想,而且感觉很脏.我现在很酷,对另一种方法并不十分感兴趣(但不要让它阻止您回答).我所要解决的是对为什么它不起作用的某种解释-它是ASP.NET MVC中的错误吗?

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?

这很难解释,所以我让我的代码(减去许多绒毛)开始讲话...希望这是有道理的!

It's hard to explain so I'll let my code (minus alot of fluff) do the talking... hope it makes sense!

编辑:似乎编译器完全忽略了第二个母版页的继承"属性-请参阅问题底部.

It seems that the compiler totally ignores the second masterpage's 'inherits' attribute - see at the bottom of the question.

ContentViewData.cs -注意它是从 BaseViewData

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

Site.Master -注意类型为 BaseViewData

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

Content.Master -注意类型为 ContentViewData 的强类型视图数据,并且它是 Site.Master 的子母版这一事实. >

Content.Master - Notice the strongly typed viewdata of type ContentViewData and the fact that it's a child masterpage of 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

<%@ 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 %>" />
<% } %>

现在,如果我尝试呈现ContentItemImage部分并像我一样引用ContentViewData对象的一个​​属性(特别是"ContentItem"属性),则在Content.Master内部.

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.

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

如果跌倒在该行上并出现以下错误

If falls over on that line with the following error

CS1061:对象"不包含"ContentItem"的定义,并且没有 扩展方法'ContentItem' 接受类型的第一个参数 可以找到对象"(您是 缺少using指令或 程序集参考?)

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?)

但是,如果我像这样进行修改,那么一切都很好,很花哨.

BUT if I change things up like so, it all works fine and dandy.

Content.Master -注意,我将整个模型(ContentViewData对象)传递给RenderPartial(),而不是尝试引用ContentViewData对象上的属性

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

<%@ 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 -注意将MyCMS.Data.Models.Content的强类型化视图数据更改为ContentViewData类.

ContentItemImage.ascx - notice the changed strongly typed viewdata from MyCMS.Data.Models.Content to the ContentViewData class.

<%@ 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.

预先感谢,查尔斯.

有趣的是,似乎编译器完全忽略了第二个母版页的'inherits'属性.

Interestingly it seems that the compiler totally ignores the second master page's 'inherits' attribute.

例如.我可以做到,它仍然可以毫无抱怨地编译...

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>" %>

推荐答案

从我的编辑中可以看到,似乎编译器完全忽略了嵌套母版页的'inherits'属性.

As you can see from my edit, it seems that the compiler totally ignores the nested master page's 'inherits' attribute.

这使我相信ASP.NET MVC中的嵌套母版将始终从其父母版继承,并且据我所知,完全忽略了继承属性.

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.

这里肯定有一些不可思议的事情...如果我删除'inherits'属性,它将无法编译,因为它不了解HtmlHelper类.但是,如果我在其中具有'inherits'属性,并且其中包含垃圾内容,它将进行编译.

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. But if I have the 'inherits' attribute in there with garbage inside it, it does compile.

不起作用

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

可以工作

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

确实很奇怪.

这篇关于强类型母版页多态性-嵌套母版页忽略继承属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 01:01