views:

16

answers:

1

I'm trying to create nested master pages in MVC. In the main master page I have a partial view that is rendered using Html.RenderPartial. This works fine when using the main master page directly in my view. The problem occurs when I have a child master page of the main master page. When using the child master page the RenderPartial method is not working. Code is below.

Is this a limitation of RenderPartial?

Main Master page -

    <%@ Master Language="VB" Inherits="System.Web.Mvc.ViewMasterPage"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
     <title></title>

     <style type="text/css">

     html
     {
           background-color:gray;
     }

     .column
     {
          float:left;
          width:300px;
          border:solid 1px black;
          margin-right:10px;
          padding:5px;
          background-color:white;

          min-height:500px;
     }

     </style>

     <asp:ContentPlaceHolder ID="head" runat="server">
     </asp:ContentPlaceHolder>
</head>
<body>

     <h1>My Website</h1>

     <div class="column">
          <asp:ContentPlaceHolder ID="Column1Content" runat="server">
          </asp:ContentPlaceHolder>
     </div>
     <div class="column">
          <asp:ContentPlaceHolder ID="Column2Content" runat="server">
          <%Html.RenderPartial("TestControl")%>
          </asp:ContentPlaceHolder>
     </div>

</body>
</html>

Child Master Page -

  <%@ Master Language="VB" Inherits="System.Web.Mvc.ViewMasterPage" MasterPageFile="~/Views/ViewJob/Parent.Master" %>
<asp:Content ID="head" ContentPlaceHolderID="head" runat="server">
    <asp:ContentPlaceHolder ID="head" runat="server" >
</asp:ContentPlaceHolder>
</asp:Content>

<asp:Content ID="ContentPlaceHolder1" ContentPlaceHolderID="Column1Content" runat="server" >
    <b>This is from the child master!!!</b>
    <asp:ContentPlaceHolder ID="Column1Content" runat="server" />
</asp:Content>

<asp:Content ID="ContentPlaceHolder2" ContentPlaceHolderID="Column2Content" runat="server">
        <asp:ContentPlaceHolder ID="Column2Content" runat="server" >
    </asp:ContentPlaceHolder>
</asp:Content>
+1  A: 

You have your RenderPartial inside your ContentPlaceHolder in the main master page, and your child master page is overwriting that RenderPartial.

Change this:

 <h1>My Website</h1>

 <div class="column">
      <asp:ContentPlaceHolder ID="Column1Content" runat="server">
      </asp:ContentPlaceHolder>
 </div>
 <div class="column">
      <asp:ContentPlaceHolder ID="Column2Content" runat="server">
      <%Html.RenderPartial("TestControl")%>
      </asp:ContentPlaceHolder>
 </div>

to this:

 <h1>My Website</h1>

 <div class="column">
      <asp:ContentPlaceHolder ID="Column1Content" runat="server">
      </asp:ContentPlaceHolder>
 </div>
 <div class="column">
      <%Html.RenderPartial("TestControl")%>
      <asp:ContentPlaceHolder ID="Column2Content" runat="server">
      </asp:ContentPlaceHolder>
 </div>

Martin
Thanks that worked!!
Quadwwchs