嵌套 MVC 母版页
我正在使用 MVC 开发 Web 应用程序,并且需要在我的站点中使用嵌套 MasterPage,以便共享可视组件。
我有两个母版页和一个 ContentPage:
- Parent.master
- Child.master
- Content.aspx
我想从以 Child.master 作为 MasterPage 的内容视图引用放置在顶部 Parent.master 上的 ContentPlaceHolder。看来我可以使用直接父级的 ContentPlaceHolders,但不能使用间接父级的 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<我的页面中的 /code> 和
Body content 2
,但不是 页面标题
。
I'm, using MVC to develop a Web application, and I need to use nested MasterPages in my site, in order to share the Visual Components.
I have two Master Pages and a ContentPage:
- Parent.master
- Child.master
- Content.aspx
I want to reference a ContentPlaceHolder placed on the top Parent.master from the Content view that has Child.master as MasterPage. It seems that I can use the ContentPlaceHolders from the direct parent, but not from the indirect parent. Let's see with a sample:
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>
The result is that I can see Body content 1
and Body content 2
in my page, but not the page title
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内容占位符仅引用其直接父级中的内容占位符。将您的 Child.master 更改为:
因此 Child.master 本质上就像标题内容占位符的“传递”。
The content place holder will only refer to the content placeholders in its immediate parent. Change your Child.master this this:
So the Child.master essentially acts like a "pass-through" for the Title content placeholder.
我很确定您必须在 child.master
和您的视图中添加标题占位符
I'm pretty sure you have to add your title place holder in your child.master
and in your view