Asp.net mvc 嵌套MVC母版页

Asp.net mvc 嵌套MVC母版页,asp.net-mvc,asp.net-mvc-2,master-pages,contentplaceholder,Asp.net Mvc,Asp.net Mvc 2,Master Pages,Contentplaceholder,我正在使用MVC开发一个Web应用程序,我需要在我的站点中使用嵌套的母版页,以便共享可视化组件 我有两个母版页和一个内容页: 母版 少儿教师 Content.aspx 我想从将Child.master作为母版页的内容视图中引用放置在顶部Parent.master上的ContentPlaceHolder。似乎我可以使用直接父级的ContentPlaceholder,但不能使用间接父级的ContentPlaceholder。让我们看一个例子: 父级.母级 <%@ Master Langua

我正在使用MVC开发一个Web应用程序,我需要在我的站点中使用嵌套的母版页,以便共享可视化组件

我有两个母版页和一个内容页:

  • 母版
  • 少儿教师
  • Content.aspx
我想从将Child.master作为母版页的内容视图中引用放置在顶部Parent.master上的ContentPlaceHolder。似乎我可以使用直接父级的ContentPlaceholder,但不能使用间接父级的ContentPlaceholder。让我们看一个例子:

父级.母级

<%@ 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>
<%@ 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>

儿童.主人

<%@ 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>
<%@ 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>

页面标题
正文内容1

内容占位符将仅引用其直接父项中的内容占位符。更改您的孩子。掌握以下内容:

<%@ 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实际上就像是标题内容占位符的“传递”。

我很确定您必须在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>   

页面标题
正文内容1

相关问题这不起作用,失败的原因是:“在母版页“~/Views/Shared/Child.master”中找不到ContentPlaceHolder‘Title’,是的,但是如果你有很多部分,这有点烦人。无论如何,谢谢。这很烦人,但唉,母版页就是这样工作的:@{View.Title=“My Title”;)注意,在MVC3中使用Razor,你可以这样做:@{View.Title=“My Title”;}在你的视野的顶端。