title: Partial view error Object reference not set to an instance of an object. MVC4 Razor
body: <p>i am doing a project to create a CMS for a school with MVC4 and Razor. I need to create a partial view on the index (for latest news and staff) and get at most 5 records for each of these Models i created a partial view in my shared folder  but i kept having error message like this</p>
  
  <blockquote>
    <p>Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <pre><code>Line 1:  @model IEnumerable&lt;MvcDale.Models.StaffModels&gt;
  Line 2:  @foreach ( var staff in Model ){
  Line 3:      &lt;div class="wrapper p4-1"&gt;
  Line 4:          &lt;figure class="img-indent3 box1"&gt;
  </code></pre>
  
  <blockquote>
    <p>Source File: c:\Users\Hammed\Documents\Visual Studio 2010\Projects\MvcDale\MvcDale\Views\Shared_StaffListPartial.cshtml    Line: 2 </p>
  </blockquote>
  
  <p>this is what my partial view file is looking like</p>
  
  <pre><code>@model IEnumerable&lt;MvcDale.Models.StaffModels&gt;
  @foreach ( var staff1 in Model ){
      &lt;div class="wrapper p4-1"&gt;
          &lt;figure class="img-indent3 box1"&gt;
              &lt;img src="@Url.Content(staff1.Picture)" alt=""&gt;
          &lt;/figure&gt;
          &lt;div class="extra-wrap"&gt;
              &lt;h6&gt;&lt;a href="#"&gt;@staff1.title @staff1.sname @staff1.oname @staff1.fname&lt;/a&gt;&lt;/h6&gt;
              @staff1.profile 
          &lt;/div&gt;
      &lt;/div&gt;
      }
  }
  </code></pre>
  
  <p>And from my home index view i have the call to it like this</p>
  
  <pre><code>{
     @Html.Partial("~/Views/Shared/_StaffListPartial.cshtml")
  }
  </code></pre>
  
  <p>can somebody help me out or suggest to me what is wrong with my code. just doing ASP.net MVC for the first time. thanks</p>
  
  <p>@Dmytro</p>
  
  <p>this my staffModel class</p>
  
  <pre><code>using System.Data.Entity;
  
  namespace MvcDale.Models
  {
      [Table("Staff")]
      public class StaffModels
      {
          [Key]
          [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
          public int staff_id { get; set; }
          public string title { get; set; }
          public string fname {get; set;}
          public string sname {get; set;}
          public string oname {get; set;}
          public string phone { get; set; }
          public string email { get; set; }
          public string profile { get; set; }
          public string Picture { get; set; }
  
      }
  }
  </code></pre>
  
  <p>this is my index.cshtml from ~/views/home/index</p>
  
  <pre><code>@{ Layout = "~/Views/Shared/_LayoutPage1.cshtml"; }
  
  
  @{
      ViewBag.Title = "Home Page";
      var Content = ViewBag.Content;
  }
  
  @section feature {
  &lt;div class="grid_11"&gt;
  &lt;div&gt;
  @Content;
  &lt;/div&gt;
  
                                  &lt;div class="indent-bottom10 indent-top5"&gt;
                                      &lt;h2 class="p5"&gt;&lt;span class="color-1"&gt;We are among&lt;/span&gt; the leading research and teaching institutions of the world!&lt;/h2&gt;
                                      &lt;div class="wrapper"&gt;
                                          &lt;div class="col-3-1"&gt;
                                              &lt;h4 class="p2"&gt;Education Projects&lt;/h4&gt;
                                              &lt;ul class="list-1 p4-1"&gt;
                                                  &lt;li&gt;&lt;a href="#"&gt;Education and Jobs&lt;/a&gt;&lt;/li&gt;
                                                  &lt;li&gt;&lt;a href="#"&gt;Public School Facts&lt;/a&gt;&lt;/li&gt;
                                                  &lt;li&gt;&lt;a href="#"&gt;International Studies&lt;/a&gt;&lt;/li&gt;
                                                  &lt;li&gt;&lt;a href="#"&gt;Public Engagement&lt;/a&gt;&lt;/li&gt;
                                                  &lt;li class="last-item"&gt;&lt;a href="#"&gt;State Testing Data&lt;/a&gt;&lt;/li&gt;
                                              &lt;/ul&gt;
                                              &lt;a href="#" class="button button1"&gt;View All&lt;span&gt;&lt;/span&gt;&lt;/a&gt;
                                          &lt;/div&gt;
                                          &lt;div class="col-3-1 font-1"&gt;
                                              &lt;h4 class="p2"&gt;General Info&lt;/h4&gt;
                                              &lt;p class="p5-1"&gt;Feipsumorbi nunc odiovia suorem aecena stiq cumsan malestonsetue adipiscing elit. Dolor&amp;shy;sedum. Mauris fermen tum did oreealiquam leotum dictum magna. Sed oreet aliquam leo. Ut tellus dolor&lt;/p&gt;
                                              &lt;a href="#" class="button"&gt;Read More&lt;span&gt;&lt;/span&gt;&lt;/a&gt;
                                          &lt;/div&gt;
                                          &lt;div class="col-3-1 font-1 last-item"&gt;
                                              &lt;h4 class="p2"&gt;Partners Programs&lt;/h4&gt;
                                              &lt;p class="indent-right4 p5-1"&gt;Nunc massa suorena stiq cumalest onsetuer adipi&amp;shy;scing elit. Mauris fermen tum did oreet aliquam leo. Ut tellus dolor dapibuso eget elementum vel curus eleife elit. &lt;/p&gt;
                                              &lt;a href="#" class="button"&gt;Read More&lt;span&gt;&lt;/span&gt;&lt;/a&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                                  &lt;div class="indent-right12"&gt;
                                      &lt;h2 class="p6"&gt;&lt;a href="#"&gt;Latest News&lt;/a&gt;&lt;/h2&gt;
                                      &lt;div class="wrapper font-1"&gt;
                                          &lt;figure class="img-indent img-indent-none3"&gt;
                                              &lt;img src="~/Images/teachers.png" alt="" /&gt;
                                          &lt;/figure&gt;
                                          &lt;div class="indent-top1 extra-wrap extra-wrap-none2 "&gt;
                                              &lt;p class="p2-1"&gt;Many surgeons are seriously affected on an emotional level when complications occur in the operating theatre, a study finds&amp;shy;&lt;a href="#" class="button"&gt;Read More&lt;span&gt;&lt;/span&gt;&lt;/a&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
  }
  
  @section staff_{
  &lt;h3 class="p3-1"&gt;our staff&lt;/h3&gt;
   @model MvcDale.Models.GenModels
      @foreach ( var staff1 in Model.Staff)
      {
          @Html.Partial("~/Views/Shared/_StaffListPartial.cshtml", staff1)
  
      }                                                                                       
  
                                  &lt;a href="#" class="link"&gt;view all&lt;/a&gt;
  
  }
  </code></pre>
  
  <p>as this is my staff patial view i intend in generating a list of 5(atmost) from</p>
  
  <pre><code>@model MvcDale.Models.StaffModels
  
      &lt;div class="wrapper p4-1"&gt;
          &lt;figure class="img-indent3 box1"&gt;
              &lt;img src="@Url.Content(Model.Picture)" alt=""&gt;
          &lt;/figure&gt;
          &lt;div class="extra-wrap"&gt;
              &lt;h6&gt;&lt;a href="#"&gt;@Model.title @Model.sname @Model.oname @Model.fname&lt;/a&gt;&lt;/h6&gt;
              @Model.profile 
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>This is what i intend to archieve
  all model and dbcontext are in order i can crude the db table already</p>