加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 嵌套的MVC母版页

发布时间:2020-12-16 03:52:35 所属栏目:asp.Net 来源:网络整理
导读:我是,使用MVC开发Web应用程序,我需要在我的站点中使用嵌套的MasterPages,以便共享Visual Components. 我有两个母版页和一个ContentPage: Parent.master Child.master Content.aspx 我想从Content视图中引用位于顶级Parent.master上的ContentPlaceHolder,该
我是,使用MVC开发Web应用程序,我需要在我的站点中使用嵌套的MasterPages,以便共享Visual Components.

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

> Parent.master
> Child.master
> Content.aspx

我想从Content视图中引用位于顶级Parent.master上的ContentPlaceHolder,该视图具有Child.master作为MasterPage.似乎我可以使用直接父级的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内容1和Body内容2,但不能看到页面标题.

解决方法

内容占位符仅引用其直接父级中的内容占位符.改变你的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实际上就像Title内容占位符的“传递”一样.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读