我是,使用MVC开发Web应用程序,我需要在我的站点中使用嵌套的MasterPages,以便共享Visual Components.
> Parent.master
> Child.master
> Content.aspx
我想从Content视图中引用位于顶级Parent.master上的ContentPlaceHolder,该视图具有Child.master作为MasterPage.似乎我可以使用直接父级的ContentPlaceHolders,但不能使用间接父级.让我们看一下样本:
Parent.master
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage"%> <HTML> <head runat="server"> <title> <asp:contentplaceholder id="Title" runat="server" /> </title> </head> <body> <asp:contentplaceholder id="Body" runat="server" /> </body> <HTML>
Child.Master
<%@ Master Language="C#" MasterPageFile="~/Views/Shared/Parent.master" Inherits="System.Web.Mvc.ViewMasterPage"%> <asp:Content ID="BodyContent" ContentPlaceHolderID="Body" runat="server"> <asp:contentplaceholder id="Body1" runat="server" /> <asp:contentplaceholder id="Body2" runat="server" /> </asp:Content>
Content.aspx
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Child.master" Inherits="System.Web.Mvc.ViewPage" %> <asp:Content ID="TitleContent" ContentPlaceHolderID="Title" runat="server"> <!-- Placed to the top parent Master page (does not work) --> The page title </asp:Content> <asp:Content ID="Body1Content" ContentPlaceHolderID="Body1" runat="server"> <!-- Placed in the direct parent Master page (Works) --> Body content 1 </asp:Content> <asp:Content ID="Body2Content ContentPlaceHolderID="Body2" runat="server"> <!-- Placed in the direct parent Master page (Works) --> Body content 2 </asp:Content>
解决方法
内容占位符仅引用其直接父级中的内容占位符.改变你的Child.master这个:
<%@ Master Language="C#" MasterPageFile="~/Views/Shared/Parent.master" Inherits="System.Web.Mvc.ViewMasterPage"%> <asp:Content ID="BodyContent" ContentPlaceHolderID="Body" runat="server"> <asp:Content ContentPlaceHolderID="Title" runat="server"> <asp:contentplaceholder id="TitleContent" runat="server" /> </asp:Content> <asp:contentplaceholder id="Body1" runat="server" /> <asp:contentplaceholder id="Body2" runat="server" /> </asp:Content>
因此,Child.master实际上就像Title内容占位符的“传递”一样.