title: How to define and render sections conditionally in Layout
body: <p>How can one define a section and render it immediately in the layout page?</p>
  
  <p>//_Layout.cshtml</p>
  
  <pre><code>&lt;header&gt;
  @section sb233{
      &lt;div class="somecontent"&gt;
      @ViewBag.somecontent
      &lt;/div&gt;
  }
  @if (ViewBag.sb233) { RenderSection("sb233"); }
  &lt;/header&gt;
  //Main container
  &lt;footer&gt;@RenderSection("footer",false)&lt;/footer&gt;    
  </code></pre>
  
  <p>//aView.cshtml</p>
  
  <pre><code>@{
    ViewBag.sb233=false; 
    ViewBag.somecontent="sb233";
   }
  //Page content
  @section footer { @RenderSection("sb233");}
  </code></pre>
  
  <p>//anotherView.cshtml</p>
  
  <pre><code>@{ViewBag.sb233=true;}
  //Page content
  @section footer { Something else}
  </code></pre>
  
  <p>Gives me a "Section Not Defined" error. </p>
  
  <p>The reason for the section to be defined in the layout rather than the view is it is a common recurring section and to allow the view to control whether and where the section should be rendered.</p>