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?

Related posts

Recent Viewed