模仿ContentPlaceHolder来定义RenderSection声明中的内容

时间:2012-06-15 14:35:24

标签: asp.net asp.net-mvc-3 razor render

因此,使用用于MVC3框架的aspx渲染引擎,很容易在主布局页面中定义一个部分,并在每个页面上显示的那些部分中插入html或asp代码,如下所示:

在主版面

<!-- Secondary content -->
<div id="content-secondary">
  <asp:ContentPlaceHolder ID="NavigationSecondary" runat="server">
    <% Html.RenderAction("Menu", "Navigation", new { @id = "nav-secondary"}); %>
  </asp:ContentPlaceHolder>
  <asp:ContentPlaceHolder ID="cphSecondary" runat="server" />
</div>
<!-- /Secondary content -->

您可以看到在ContentPlaceHolder中呈现了一个名为NavigationSecondary的菜单。因此,在我创建的每个页面上,默认显示菜单,并在其下方显示任何其他额外内容。

现在,我如何在Razor引擎中解释这一点?我无法在网上找到太多信息。我找到了一些显示如何使用默认内容的内容。但是,当插入来自其他页面的内容时,该默认内容是否会被删除?

1 个答案:

答案 0 :(得分:1)

Razor视图引擎允许检查(在_Layout.cshtml中)布局部分是否已由视图实现,因此您可以模仿将代码放入ContentPlaceHolder

<div id="content-secondary">
    @if (IsSectionDefined("NavigationSecondary"))
    {
        Html.RenderAction("Menu", "Navigation", new {@id = "nav-secondary"});
        @RenderSection("NavigationSecondary")
    }
</div>

希望这就是你要找的东西。

相关问题