title: MVC 5 show Partial-View in Layout only on Home Index
body: <p>I use a sidebar on my website with stats from my database and statics data like links and other texts.</p>
  
  <p>In my <code>_Layout.cshtml</code>, I use <code>Html.RenderAction("SidebarPV", "Home");</code> to call the sidebar.</p>
  
  <p>The sidebar is a Partial-View using a ViewModel for the stats.
  <code>SidebarPV</code> is generated in my <code>HomeController</code> like that :</p>
  
  <pre class="lang-cs prettyprint-override"><code>public ActionResult SidebarPV() {
      SidebarViewModel viewmodel = new SidebarViewModel();
      DateTime now = DateTime.Now;
      viewmodel.stat_data1 = db.Table1.Where(e =&gt; e.DateDeb &lt;= now &amp;&amp; e.DateFin &gt;= now).Count();
      viewmodel.stat_data2 = db.Table2.Where(c =&gt; c.DateDeb &lt;= now &amp;&amp; c.DateFin &gt;= now).Count();
      return PartialView("SidebarPV", viewmodel);
  }
  </code></pre>
  
  <p>It works like a charm but I don't need stats on all views, only on <code>/Home/Index</code></p>
  
  <p>So I want to 'comment' the stats generation when the ser is not on the index of the website.</p>
  
  <p>Thanks for advices.</p>
  
  <p><strong>EDIT (solution, thanks to <a href="https://stackoverflow.com/users/1195056/krillgar">krillgar</a>) :</strong></p>
  
  <p>I wrote in my <code>_Layout</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>@{
      var isHome = ViewContext.RouteData.Values["controller"].ToString().ToUpper() == "HOME";
      var isIndex = ViewContext.RouteData.Values["action"].ToString().ToUpper() == "INDEX";
      if (isHome &amp;&amp; isIndex) {
          Html.RenderAction("SidebarPV", "Home");
      }
      else {
          Html.RenderAction("SidebarNoStatPV", "Home");
      }
  }
  </code></pre>
  
  <p>I know I need to create two partial-views but one is static so I will not edit it for a long time :).
  Tested, It works.</p>