Here's the situation I'm trying to solve:
I have a main shared layout with a single section @RenderSection("Menu") that I want to default to a standard menu for the majority of my pages, but I would like to replace/override that section in a few sub pages. Is this possible with Razor at this stage in the game?
I was hoping I could possibly define the default implementation of that section in _ViewStart.cshtml but it doesn't seem to like it.
Would a Menu Partial view be better for this situation?
Edit:
I'm getting the following error with this code now: The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "menu".
_Layout.cshtml
<div id="menu">
@if (IsSectionDefined("menu"))
{
RenderSection("menu");
}
else {
<text>
<ul>
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li><a href="#">Lookups</a>
<ul>
@Html.ActionLink("Product","Index","Product")
</ul>
</li>
</ul>
</text>
}
</div>
Index.cshtml
@section menu {
<ul>
<li>@Html.ActionLink("Product", "Index", "Product")</li>
<li>@Html.ActionLink("Form Type", "Index", "Product")</li>
<li>@Html.ActionLink("Supplier", "Index", "Product")</li>
</ul>
}
Best Answer
Ah... I was messing with this and found I could do it by just switching the logic around:
So if my pages don't define
@section Header { ... }
, it renders the default.