title: ASP.net MVC 4 loading menu from database to Partial View
body: <p>i'm an mvc newbie.</p>
  
  <p>What i'm trying to do is to load menu from database and display it in partial view. This partial view will be called from _layout view.</p>
  
  <p><strong>Home Controller</strong></p>
  
  <p>in Home controller i add an action called "_MainMenu"</p>
  
  <pre><code>public class HomeController : Controller
  {
          mrpDatabase _db = new mrpDatabase();
  
          public ActionResult _MainMenu()
          {
            return PartialView("_MainMenu", _db.menu.ToList());
          }
          ....
  }
  </code></pre>
  
  <p><strong>PartialView</strong></p>
  
  <p>this is my _MainMenu PartialView</p>
  
  <pre><code>@model IEnumerable&lt;appMRP.Models.menu&gt;
  &lt;ul id="menu"&gt;
      @foreach (var item in Model)
      {
          &lt;li&gt;@item.menu1&lt;/li&gt;
      }                                            
  &lt;/ul&gt;
  </code></pre>
  
  <p><strong>Layout Page</strong></p>
  
  <p>this partial menu is displayed in my _Layout.cshtml like this</p>
  
  <pre><code>&lt;nav&gt;                     
    @Html.Partial("_MainMenu")
  &lt;/nav&gt;
  </code></pre>
  
  <p>when i run this. i got error 
  "<strong>NullReferenceException was unhandled by user code.
  Object reference not set to an instance of an object</strong>"</p>
  
  <p>seems the "Model" in my _MainMenu is null</p>
  
  <p>what did i do wrong ?</p>
  
  <p>thank you</p>