我使用MVC开发Web应用程序,我需要在我的站点中使用嵌套的MasterPages,以便共享Visual Components。
我有两个母版页和一个ContentPage:
我想从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>
结果是我可以在页面中看到Body content 1
和Body content 2
,但不能看到page title
。
答案 0 :(得分:5)
内容占位符仅引用其直接父级中的内容占位符。改变你的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基本上就像标题内容占位符的“传递”一样。
答案 1 :(得分:0)
我很确定你必须在你的child.master
中添加你的标题占位符<%@ Master Language="C#" MasterPageFile="~/Views/Shared/Parent.master"
Inherits="System.Web.Mvc.ViewMasterPage"%>
<asp:Content ID="TitleContent" ContentPlaceHolderID="Title" runat="server" />
<asp:Content ID="BodyContent" ContentPlaceHolderID="Body" runat="server">
<asp:contentplaceholder id="Body1" runat="server" />
<asp:contentplaceholder id="Body2" runat="server" />
</asp:Content>
并在您的视图中
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Child.master"
Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="TitleContent1" ContentPlaceHolderID="TitleContent" 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>