6

Tôi đang sử dụng MVC để phát triển một ứng dụng Web và tôi cần sử dụng các MasterPages lồng nhau trong trang web của mình, để chia sẻ các thành phần Visual.Các trang cái của MVC lồng nhau

Tôi có hai Master Pages và một ContentPage:

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

Tôi muốn tham khảo một ContentPlaceHolder đặt lên hàng đầu Parent.master từ chế độ xem Nội dung có Child.master dưới dạng MasterPage. Có vẻ như tôi có thể sử dụng ContentPlaceHolders từ cha mẹ trực tiếp, nhưng không phải từ cha mẹ gián tiếp. Hãy xem với một mẫu:

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> 

Kết quả là tôi có thể thấy Body content 1Body content 2 trong trang của tôi, nhưng không thể xem page title.

+0

Câu hỏi liên quan http://stackoverflow.com/questions/947134/are-there-nested-master-pages-in-asp-net-mvc –

Trả lời

5

Trình giữ chỗ nội dung sẽ chỉ đề cập đến trình giữ chỗ nội dung trong số ngay lập tức cấp độ gốc của chúng. Thay đổi Child.master của bạn thành phần này:

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

Vì vậy, Child.master về cơ bản hoạt động giống như "thông qua" cho phần giữ chỗ Nội dung tiêu đề.

+0

Có, nhưng điều này hơi khó chịu nếu bạn có nhiều phần. Cảm ơn anyway –

+0

Thật khó chịu, nhưng than ôi, đó là cách trang chủ hoạt động. :) Lưu ý, với Razor trong MVC3 bạn chỉ có thể làm điều này: @ {View.Title = "My title"; } ở đầu chế độ xem của bạn. –

0

Tôi khá chắc chắn rằng bạn phải thêm giữ chỗ tiêu đề của bạn trong child.master bạn

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

và theo quan điểm của bạ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

Điều này không hoạt động, nó không thành công với: "Không thể tìm thấy ContentPlaceHolder 'Tiêu đề' trong trang chủ '~/Views/Shared/Child.master' –

Các vấn đề liên quan