title: Object reference not set to an instance of an object in Html.Partial MVC when load data
body: <p>I have _layout.cshtml page in mvc project that call partial view with this code</p>
  
  <blockquote>
    <p>@Html.Partial("_MenuForAdmin")</p>
  </blockquote>
  
  <p><strong>The code for MenuModel</strong> :</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.ComponentModel.DataAnnotations;
  using System.Text;
  
  namespace i_insurance.Models
  {
      public class MenuModel
      {
          public List&lt;string&gt; li { get; set; }
  
      }
  }
  </code></pre>
  
  <p><strong>The code for MenuController</strong> :</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  
  using i_insurance.Models;
  using i_insurance.DataAccess;
  using System.Data;
  
  namespace i_insurance.Controllers
  {
      public class MenuController : Controller
      {
  
          public ActionResult Index()
          {
              MenuModel model = new MenuModel();
              model.li = LoadUl();
              return View(model);
          }
  
          public List&lt;string&gt; LoadUl()
          {
              List&lt;string&gt; list = new List&lt;string&gt;();
              list.Add("Testing");
              list.Add("MCV");
              list.Add("Project");
              return list;
          }
  
      }
  }
  </code></pre>
  
  <p><strong>The code for partial view _MenuForAdmin.cshtml</strong> :</p>
  
  <blockquote>
    <p>@model i_insurance.Models.MenuModel</p>
  
  <pre><code>@foreach (var item in Model.li) {
  &lt;text&gt;
      item
  &lt;/text&gt; }
  </code></pre>
  </blockquote>
  
  <p><strong>The code for Index.cshtml inside home folder</strong> :</p>
  
  <pre><code>@{
      ViewBag.Title = "Home Page";
  }
  @section featured {
      &lt;section class="featured"&gt;
          &lt;div class="content-wrapper"&gt;
              &lt;hgroup class="title"&gt;
                  &lt;h1&gt;@ViewBag.Title.&lt;/h1&gt;
                  &lt;h2&gt;@ViewBag.Message&lt;/h2&gt;
              &lt;/hgroup&gt;
  
          &lt;/div&gt;
      &lt;/section&gt;
  }
  &lt;h3&gt;We suggest the following:&lt;/h3&gt;
  &lt;ol class="round"&gt;
      &lt;li class="one"&gt;
          &lt;h5&gt;Getting Started&lt;/h5&gt;
          ASP.NET MVC gives you a powerful
      &lt;/li&gt;
  &lt;/ol&gt;
  </code></pre>
  
  <p>and when i run this project in browser it show "Object reference not set to an instance of an object" , here is the </p>
  
  <blockquote>
  <pre><code>Source Error:
  
  Line 1:  @model i_insurance.Models.MenuModel
  Line 2:  
  Line 3:  @foreach (var item in Model.li)
  Line 4:  {
  Line 5:      &lt;text&gt;
  
  Source File: d:\Projects\VS2012\Website\i_insurance\i_insurance\Views\Shared\_MenuForAdmin.cshtml
  </code></pre>
    
    <p>Line: 3</p>
  </blockquote>
  
  <p><strong>Stack Trace</strong>:</p>
  
  <pre><code>[NullReferenceException: Object reference not set to an instance of an object.]
     ASP._Page_Views_Shared__MenuForAdmin_cshtml.Execute() in d:\Projects\VS2012\Website\i_insurance\i_insurance\Views\Shared\_MenuForAdmin.cshtml:3
     System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
     System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +103
     System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +88
     System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
     System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
     System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection) +277
     System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +91
     System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName) +32
     ASP._Page_Views_Home_Index_cshtml.Execute() in d:\Projects\VS2012\Website\i_insurance\i_insurance\Views\Home\Index.cshtml:24
     System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
     System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +103
     System.Web.WebPages.StartPage.RunPage() +17
     System.Web.WebPages.StartPage.ExecutePageHierarchy() +62
     System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
     System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
     System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
     System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
     System.Web.Mvc.&lt;&gt;c__DisplayClass1a.&lt;InvokeActionResultWithFilters&gt;b__17() +23
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +245
     System.Web.Mvc.&lt;&gt;c__DisplayClass1c.&lt;InvokeActionResultWithFilters&gt;b__19() +22
     System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +176
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass2a.&lt;BeginInvokeAction&gt;b__20() +75
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass25.&lt;BeginInvokeAction&gt;b__22(IAsyncResult asyncResult) +99
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
     System.Web.Mvc.&lt;&gt;c__DisplayClass1d.&lt;BeginExecuteCore&gt;b__18(IAsyncResult asyncResult) +14
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;b__3(IAsyncResult ar) +23
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
     System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;b__3(IAsyncResult ar) +23
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
     System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
     System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
     System.Web.Mvc.&lt;&gt;c__DisplayClass8.&lt;BeginProcessRequest&gt;b__3(IAsyncResult asyncResult) +25
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;b__3(IAsyncResult ar) +23
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
     System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
     System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9634212
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155
  </code></pre>
  
  <p>my question is why this happen ? and how to solve it ?</p>