title: ASP.Net MVC Controller for _Layout
body: <p>I am working on a dynamic menu system for MVC and just to make it work, I created a partial view for the menu and it works great using the syntax below:</p>
  
  <pre><code>@Html.RenderPartial("_Menu", (Models.Menu)ViewBag.MainMenu)
  </code></pre>
  
  <p>BUT, to do so, I would have to set the MainMenu and FooterMenu (or any other menu for that matter) in the ViewBag on each Controller and each action.  To avoid this, I was wondering if there was a recommended event that I could access the ViewBag globally.  If not, does anyone recommend passing the Menu object into a session variable?  It doesn't sound right to me, but only thing I can think of right now.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>_Layout.cshtml - I included the new call to Action:</p>
  
  <pre><code>@Html.Action("RenderMenu", "SharedController", new { name = "Main" })
  </code></pre>
  
  <p>SharedController.cs - Added New Action:</p>
  
  <pre><code>public ActionResult RenderMenu(string name)
  {
      if (db.Menus.Count&lt;Menu&gt;() &gt; 0 &amp;&amp; db.MenuItems.Count&lt;MenuItem&gt;() &gt; 0)
      {
          Menu menu = db.Menus.Include("MenuItems").Single&lt;Menu&gt;(m =&gt; m.Name == name);
          return PartialView("_MenuLayout", menu);
      }
      else
      {
          return PartialView("_MenuLayout", null);
      }
  }
  </code></pre>
  
  <p>And it throws the following exception:</p>
  
  <blockquote>
    <p>The controller for path '/' was not found or does not implement
    IController.</p>
  </blockquote>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>So, the issue is that I referenced the Controller by the full name and you only need the name of the controller minus "Controller".  Neat tidbit.  So, for my example, this works:</p>
  
  <pre><code>@Html.Action("RenderMenu", "Shared", new { name = "Main" })
  </code></pre>