6

我是使用MVC開發一個Web應用程序,並且我需要在我的網站中使用嵌套的MasterPages,以共享Visual Components。嵌套的MVC母版頁

我有兩個母版頁和ContentPage:

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

我要引用放置在頂部的ContentPlaceHolder從具有Child.master作爲MasterPage的內容視圖的Parent.master。看起來我可以使用直接父代中的ContentPlaceHolders,但不能使用間接父代中的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

+0

相關問題http://stackoverflow.com/questions/947134/are-there-nested-master-pages-in-asp-net-mvc – 2010-11-11 14:24:46

回答

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本質上就像標題內容佔位符的「傳遞」。

+0

是的,但如果你有很多部分,這有點煩人。無論如何,謝謝 – 2010-11-11 14:44:25

+0

這很煩人,但唉,這就是masterpages如何工作。 :)注意,使用MVC3中的Razor,你可以這樣做:@ {View.Title =「My title」; }在您的視圖的頂部。 – 2010-11-11 15:37:37

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

這不起作用,它會失敗:「在主頁面'〜/ Views/Shared/Child.master'中找不到ContentPlaceHolder'標題' – 2010-11-11 14:40:50

相關問題