Sebbo
Sebbo

Reputation: 405

Mimic ContentPlaceHolder to define content inside RenderSection declaration

So, using the aspx rendering engine for the MVC3 framework, it is easy to define a section in the master layout page and insert html or asp code inside those sections that will appear on every page, like so:

On the master layout

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

You can see that a Menu is rendered inside the ContentPlaceHolder called NavigationSecondary. So on every page that I create, the menu is displayed by default and any other extra content is displayed below that.

Now, how would I interpret this in the Razor engine? I couldn't find much info online. I did find something that shows how to use default content. But would that default content be erased when inserted with content from another page?

Upvotes: 1

Views: 399

Answers (1)

lucask
lucask

Reputation: 2290

Razor view engine allows checking (in _Layout.cshtml) if layout section has been implemented by views so with this you could mimic putting code inside ContentPlaceHolder.

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

Hope this is what you are looking for.

Upvotes: 1

Related Questions