嵌套的MVC母版页

6

我正在使用MVC开发Web应用程序,需要在我的网站中使用嵌套的MasterPages,以便共享视觉组件。

我有两个Master Pages和一个ContentPage:

  • Parent.master
  • Child.master
  • Content.aspx

我想从具有Child.master作为MasterPage的Content视图引用放置在顶部Parent.master上的ContentPlaceHolder。似乎我可以使用直接父级的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”。请注意,保留了HTML标签。

相关问题 https://dev59.com/LHNA5IYBdhLWcg3wdtz5 - John Hartsock
2个回答

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 本质上像是 Title 内容占位符的“传递器”。

是的,但如果你有很多部分,这可能有点烦人。无论如何还是谢谢。 - Daniel Peñalba
这很烦人,但唉,这就是母版页的工作方式。 :) 请注意,在MVC3中使用Razor,您只需在视图顶部执行此操作:@{ View.Title = "My title"; } - Steve Michelotti

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>   

这个不起作用,它失败了并显示:“无法在母版页'~/Views/Shared/Child.master'中找到ContentPlaceHolder 'Title'。” - Daniel Peñalba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接