1

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?

2 Answers 2

4

Syntax error with your RenderSection. Give this a try:

@if (IsSectionDefined("Navigation"))
{
    @RenderSection("Navigation", false)
}
else
{
    <p>No navigation setup!</p>
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

Oh that's a neat one! Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.