The following sections have been defined but have not been rendered for the layout page

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 = "Home";            
}

@section meta{
    <meta name="keywords" content="" />
    <meta name="description" content="" />
}

<h2>Html Content Here</h2>

@section footer {
    <script src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8"></script>
    <script type="text/javascript">
        $(document).ready(function() {
    });    
    </script> 
}

Your layout page isn't actually rendering the sections footer and meta

In your _Layout.cshtml, put in @RenderSection("meta") where you want the meta section rendered.

You can also do @RenderSection("meta", false) to indicate that the section is optional.


The error message implies that your _Layout.cshtml does not include @RenderSection statements for the @sections that you have in your view.

Check your Layout and let us know.

See this page for more information.


I faced a similar problem where the layout template indeed has the @RenderSection(...) but inside an if-else statement. So when the page executes the statement that doesn't contain the @RenderSection it will throw that exception. If that is your case, then your solution is a little more complicated, either:

  • make sure you have @RenderSection outside of the statements or
  • repeat @RenderSection in both if-else statements, or
  • use different partial views, or
  • redesign the layout.

That could be your main problem!