Nested MVC master pages
Asked Answered
T

2

6

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.

Turncoat answered 11/11, 2010 at 14:18 Comment(1)
Related Question #947634Teplitz
L
5

The content place holder will only refer to the content placeholders in its immediate parent. Change your Child.master this this:

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

So the Child.master essentially acts like a "pass-through" for the Title content placeholder.

Lelalelah answered 11/11, 2010 at 14:22 Comment(2)
Yes, but this is a little annoying if you have many sections. Thanks anywayHomocentric
It is annoying, but alas, that is how masterpages work. :) Note, with Razor in MVC3 you can just do this: @{ View.Title = "My title"; } at the top of your view.Lelalelah
C
0

I'm pretty sure you have to add your title place holder in your 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> 

and in your view

<%@ 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>   
Cabdriver answered 11/11, 2010 at 14:25 Comment(1)
This does not work, it fails with: "Cannot find ContentPlaceHolder 'Title' in the master page '~/Views/Shared/Child.master'Homocentric

© 2022 - 2024 — McMap. All rights reserved.