嵌套的MVC母版页

问题描述 投票:6回答:2

我是,使用MVC开发Web应用程序,我需要在我的站点中使用嵌套的MasterPages,以便共享Visual Components。

我有两个母版页和一个ContentPage:

  • 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>

结果是我可以在我的页面中看到Body content 1Body content 2,但不是page title

asp.net-mvc asp.net-mvc-2 master-pages contentplaceholder
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内容占位符的“传递”一样。


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>   
© www.soinside.com 2019 - 2024. All rights reserved.