Reputation: 2432
I'm trying to have the navigation bars for my website to be a little dynamic. If a categoryId is equals to 0 I remove some buttons, else I display them all:
_Layout.cshtml:
<body>
<div class="page">
<div id="header">
<div id="logindisplay">
@Html.Partial("_LogOnPartial")
</div>
<div id="menucontainer">
@if (IsSectionDefined("Navigation"))
{
{RenderSection("Navigation", false);}
}
else
{
<p>No navigation setup!</p>
}
</div>
</div>
<div id="main">
<div id="contentwrapper">
@RenderBody()
</div>
<div id="footer">
</div>
</div>
</div>
</body>
Index.cshtml: from my HomeController
@model Project2.ViewModels.ProjectCategoryListViewModel
@{
ViewBag.Title = "Home Page";
}
@section Navigation {
@{Html.RenderAction("LayoutNav", "Home", new { CategoryId = 0 });}
}
<!-- Rest of the page's code -->
Index.cshtml: from my CategoryController
@model Project2.ViewModels.Categories.CategoryIndexViewModel
@{
ViewBag.Title = "Category Index";
}
@section Navigation {
@{Html.RenderAction("LayoutNav", "Home", new { CategoryId = Model.Category.Id });}
}
<!-- Rest of the page's code -->
I tried the exact same setup but without RenderAction and just entering html directly but I keep getting this error message:
The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "Navigation".
The Index actions in the Category and Home controller are straight forward ActionResults that return a View.
Any ideas what's going on there?
Upvotes: 1
Views: 4539
Reputation: 59001
I wrote a blog post with a helper method for specifying default content in a RenderSection call: http://haacked.com/archive/2011/03/05/defining-default-content-for-a-razor-layout-section.aspx
It would let you do the following:
@RenderSection("Navigation", @<p>No navigation setup!</p>)
Upvotes: 2
Reputation: 409
Syntax error with your RenderSection. Give this a try:
@if (IsSectionDefined("Navigation"))
{
@RenderSection("Navigation", false)
}
else
{
<p>No navigation setup!</p>
}
Upvotes: 4