6

Estoy utilizando MVC para desarrollar una aplicación web y necesito utilizar páginas maestras anidadas en mi sitio para compartir los componentes visuales.Páginas maestras anidadas de MVC

tengo dos páginas principales y una ContentPage:

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

quiero hacer referencia a un ContentPlaceHolder colocado en la parte superior Parent.master desde la vista de contenido que tiene Child.master como MasterPage. Parece que puedo usar ContentPlaceHolders del padre directo, pero no del padre indirecto. Veamos con un ejemplo:

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> 

El resultado es que puedo ver Body content 1 y Body content 2 en mi página, pero no el page title.

+0

pregunta relacionada http://stackoverflow.com/questions/947134/are-there-nested-master-pages-in-asp-net-mvc –

Respuesta

5

El titular de posición de contenido solo se referirá a los marcadores de posición de contenido en su elemento primario padre. Cambiar su Child.master esta esto:

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

Así que la Child.master actúa esencialmente como un "paso a través" para el marcador de contenido Título.

+0

Sí, pero esto es un poco molesto si tiene muchas secciones. Gracias de todos modos –

+0

Es molesto, pero, por desgracia, así es como funcionan las páginas maestras. :) Nota, con Razor en MVC3 puedes hacer esto: @ {View.Title = "My title"; } en la parte superior de tu vista. –

0

Estoy bastante seguro de que tiene que añadir su lugar titular en su 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> 

y en su opinión

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

Esto no funciona, falla con: "No se puede encontrar ContentPlaceHolder 'Título' en la página maestra '~/Views/Shared/Child.master' –

Cuestiones relacionadas