title: Asp.Net MVC5 Web Pages - using @RenderSection in _Navigation view
body: <p>I have a _Navigation.cshtml view, to which I am trying to add some conditional <code>&lt;ul&gt; &lt;/ul&gt;</code> tags using the Razor <code>IsSectionDefined()</code> and <code>@RenderSection()</code> directives.</p>
  
  <p>However, I'm getting the following exception:</p>
  
  <p><code>The file "~/Views/Shared/_Navigation.cshtml" cannot be requested directly because it calls the "RenderSection" method.</code></p>
  
  <p>As you can see in my below HTML snippet, I can add <code>@RenderSection("bobtest")</code> very easily into my <code>_Layout.cshtml</code> view, but NOT in the <code>_Navigation</code> view :</p>
  
  <p>Snippet from ..\Views\Home\Index.cshtml :</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>@section bobtest{
      &lt;ul class="nav nav-second-level collapse @Html.IsSelected(controller: "Dashboards", cssClass: "in")"&gt;
      &lt;li class="@Html.IsSelected(action: "Dashboard_1")"&gt;&lt;a href="@Url.Action("Dashboard_1", "Dashboards")"&gt;Dashboard v.1&lt;/a&gt;&lt;/li&gt;
      &lt;li class="@Html.IsSelected(action: "Dashboard_2")"&gt;&lt;a href="@Url.Action("Dashboard_2", "Dashboards")"&gt;Dashboard v.2&lt;/a&gt;&lt;/li&gt;
      &lt;li class="@Html.IsSelected(action: "Dashboard_3")"&gt;&lt;a href="@Url.Action("Dashboard_3", "Dashboards")"&gt;Dashboard v.3&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here is my _Layout.cshtml, which pulls in "_Navigation", "_TopNavbar", and "_Footer"</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="wrapper"&gt;
  
          &lt;!-- Navigation --&gt;
          @Html.Partial("_Navigation")
  
          &lt;!-- Page wraper --&gt;
          &lt;div id="page-wrapper" class="gray-bg"&gt;
  
              &lt;!-- Top Navbar --&gt;
              @Html.Partial("_TopNavbar")
  
              &lt;!-- Main view  --&gt;
              @RenderBody()
  
              @RenderSection("bobtest", required: true)   &lt;!-- THIS WORKS... --&gt;
  
              &lt;!-- Footer --&gt;
              @Html.Partial("_Footer")
  
          &lt;/div&gt;
          &lt;!-- End page wrapper--&gt;
  
      &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>and here is a snippet from the <code>_Navigation</code> view:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>&lt;nav class="navbar-default navbar-static-side" role="navigation"&gt;
  &lt;div id="sidebar-collapse" class="sidebar-collapse"&gt;
  &lt;ul class="nav" id="side-menu"&gt;
  
   &lt;li class="nav-header"&gt;
  
   &lt;li class="@Html.IsSelected(controller: "Whatif")"&gt;
    &lt;a href="@Url.Action("Dashboard_1", "Dashboards")"&gt;&lt;i class="fa fa-th-large"&gt;&lt;/i&gt; &lt;span class="nav-label" data-i18n="nav.dashboard"&gt;Current Portfolios&lt;/span&gt; &lt;span class="fa arrow"&gt;&lt;/span&gt;&lt;/a&gt;
    @if (IsSectionDefined("bobtest"))
    {
     @RenderSection("bobtest", required: false)  
    }
   &lt;/li&gt;
   
   &lt;/ul&gt;
  &lt;/div&gt;
  &lt;/nav&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>It will only cause an exception here, so I need some clarity on how to add conditional tags to my _Navigation view.</p>
  
  <p>thanks,
  Bob</p>