This is an ASP.NET MVC 3 exception message. What it says? What should I do?
OK, I have this code:
@{
Layout = \"~/_Layout.cshtml\";
Page.Title
When I received this error I was trying to conditionally include a section in my layout when it was defined by the view. Here is the layout:
@if (IsSectionDefined("header"))
{
RenderSection("header");
}
else
{
Html.RenderPartial("_Header");
}
I think @Jaider mentions this in his answer too, but you cannot place a RenderSection
inside an if statement. The way to achieve my goal was to inverse the if and set the required
parameter to false on the RenderSection
method:
@RenderSection("header", false);
@if (!IsSectionDefined("header"))
{
Html.RenderPartial("_Header");
}
If no setion is defined, the first line wont do anything and the If statement ensures a default header is rendered when no section is defined.