【问题标题】:Nested MVC master pages嵌套的 MVC 母版页
【发布时间】:2010-11-11 14:18:16
【问题描述】:

我正在使用 MVC 开发 Web 应用程序,我需要在我的站点中使用嵌套的 MasterPages,以便共享可视组件。

我有两个母版页和一个 ContentPage:

  • Parent.master
  • Child.master
  • 内容.aspx

我想从以 Child.master 作为 MasterPage 的 Content 视图引用放置在 Parent.master 顶部的 ContentPlaceHolder。似乎我可以使用直接父级的 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

【问题讨论】:

标签: asp.net-mvc asp.net-mvc-2 master-pages contentplaceholder


【解决方案1】:

内容占位符只会引用其直接父级中的内容占位符。将您的 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 本质上就像标题内容占位符的“传递”。

【讨论】:

  • 是的,但是如果你有很多部分,这有点烦人。还是谢谢
  • 这很烦人,但是,这就是母版页的工作方式。 :) 注意,使用 MVC3 中的 Razor,您可以这样做:@{ View.Title = "My title"; } 在您的视图顶部。
【解决方案2】:

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

【讨论】:

  • 这不起作用,它失败了:“在母版页'~/Views/Shared/Child.master'中找不到ContentPlaceHolder'Title'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多