3

我正在尝试在 MVC 中创建嵌套母版页。在主母版页中,我有一个使用 Html.RenderPartial 呈现的局部视图。当直接在我的视图中使用主母版页时,这很好用。当我有主母版页的子母版页时,会出现此问题。使用子母版页时,RenderPartial 方法不起作用。代码如下。

这是 RenderPartial 的限制吗?

主页 -

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

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

子母版页 -

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

1 回答 1

3

您在主母版页的 ContentPlaceHolder 中有您的 RenderPartial,而您的子母版页正在覆盖该 RenderPartial。

改变这个:

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

对此:

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

于 2010-10-25T15:47:05.450 回答