Search code examples
c#asp.net-mvcrazorlayout

RenderBody() method in an if statement?


I've got a project with a lot of views that have the same layout, so I used _ViewStart to make them all have the same layout. In that layout, I use @RenderBody() to fill the layout in with the content of a page. That works.

Now I'm trying to put @RenderBody() in an if statement. In the else statement I have put some html. It looks like this:

    ...*/
    @if (condition == true) { 
        @RenderBody()
    } else {
        <p>Some text if the condition is false</p>
    }
    /*...

When I run this code and the condition is false, I get this error:

System.Web.HttpException: The "RenderBody" method has not been called for layout page "~/Views/Shared/_BasicLayout.cshtml".

So my question is:

How do I put @RenderBody() in an if statement?

On request, here is the whole _BasicLayout body:

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li @(@ViewBag.Title == "Films" ? "class=active" : "")>@Html.ActionLink("Films", "List", "FilmSelectie")</li>
                <li @(@ViewBag.Title == "Index" ? "class=active" : "")>@Html.ActionLink("Voorstellingen", "Index", "Voorstelling")</li>
                <li @(@ViewBag.Title == "Nieuwsbrief" ? "class=active" : "")>@Html.ActionLink("Nieuwsbrief", "Nieuwsbrief", "Nieuwsbrief")</li>
                <li @(@ViewBag.Title == "Contact" ? "class=active" : "")>@Html.ActionLink("Contact", "ContactBreda", "Contact")</li>
                <li @(@ViewBag.Title == "Login" ? "class=active" : "")>@Html.ActionLink("Login", "Login", "User")</li>
            </ul>
        </div>
    </div>
</nav>
<div class="row panel" style="display:block; box-shadow: none;margin-top:5px;margin:0;margin-left:0px;">
    <div id="film informatie" class="col-xs-3">
    </div>
    <div class="col-xs-8" style="display:block; box-shadow:none; margin-top:20px;">
        @if (0 == 1) { 
            @RenderBody()
        } else {
            <h2>Geen toegang!</h2>
            <p>U heeft geen toegang tot deze pagina. Log in met een geschikt account om op deze pagina te komen.</p>
        }
    </div>
</div>
<footer>
    <div class="panel-footer" style="display:block; text-align:center;">
        <p>
            © 2015 Cinema Jam || Chasséveld 1, Breda ||
            <a  href="https://www.facebook.com/CinemaJamBreda" target="_blank"><img style="width:20px; height:20px;" src="~/Content/facebook.png" alt="CinemaJam FacebookPagina"></a>
            <a href="https://twitter.com/CinemaJamB3" target="_blank"><img style="width:20px; height:20px;"  src="~/Content/twitter.png" alt="CinemaJam TwitterPagina"></a>
        </p>
    </div>
</footer>

(somehow i can not paste the header and the start of the body in stack overflow, it contains references to bootstrap)


Solution

  • The solution to this problem is to use [Authorize]. I'm following this tutorial now: http://www.codeproject.com/Articles/578374/AplusBeginner-splusTutorialplusonplusCustomplusF Thanks to DionV.