title: How do I optionally render a section in ASP.Net MVC 3?
body: <p>On my website, I have a section (a floating sidebar) that I want rendered only for a sub-set of users (admins). I'm hoping that I can put the logic in the master layout for determining if the section should be shown or not but that causes an error on the page if the section isn't rendered.</p>
  
  <p>Example code - Layout.cshtml...</p>
  
  <pre><code>... code ...
  @if(user.IsAdmin) {
      @RenderSection("AdminSidebar", false)
  }
  </code></pre>
  
  <p>Example code - MyPage.cshtml...</p>
  
  <pre><code>@section AdminSidebar {
      ... code ...
  }
  </code></pre>
  
  <p>Does anybody know how to get this to work without having to put the logic in all of the child pages?</p>
  
  <p>As a note, IsSectionDefined("AdminSidebar") only works in the layout file. It doesn't work in the page to test if the section is available or not.</p>