title: Error with foreach loop when I try to use one model for two different views
body: <p>I am very new to ASP.NET MVC so this would probably be an easy question. I followed the <a href="http://www.w3schools.com/aspnet/mvc_app.asp" rel="nofollow">W3 Schools Movies tutorial</a> and created a small database with fields Naslov ("Heading" in my language) Sadrzaj ("Content" in my language) and Datum ("Date") which I want to use as blog posts and to list on main Index. This is my current code for Aktuelnosti.cs</p>
  
  <pre><code>    namespace comp_2000.Models
  {
      public class Aktuelnosti
      {
          public int ID { get; set; }
          public string Naslov { get; set; }
          public string Sadrzaj { get; set; }
          public DateTime Datum { get; set; }
      }
      public class AktuelnostiContext : DbContext
      {
          public DbSet&lt;Aktuelnosti&gt; Aktuelnosti { get; set; }
      }
  }
  </code></pre>
  
  <p>And this is my related view for Index.cshtml in Aktuelnosti, which works</p>
  
  <pre><code>@model IEnumerable&lt;comp_2000.Models.Aktuelnosti&gt;
  
  &lt;table border="1"&gt;
      &lt;tr&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Naslov)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Sadrzaj)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Datum)
          &lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
  
          @foreach (var item in Model) {
      &lt;tr&gt;
          &lt;td width="20%"&gt;
              @Html.DisplayFor(modelItem =&gt; item.Naslov)
          &lt;/td&gt;
          &lt;td width="60%"&gt;
              @Html.DisplayFor(modelItem =&gt; item.Sadrzaj)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Datum)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
              @Html.ActionLink("Preview", "Details", new { id=item.ID }) |
              @Html.ActionLink("Delete", "Delete", new { id=item.ID })
          &lt;/td&gt;
      &lt;/tr&gt;
  </code></pre>
  
  <p>So, when I try the same code for Index.cshtml inside Home, which looks like this</p>
  
  <pre><code>@model IEnumerable&lt;comp_2000.Models.Aktuelnosti&gt;
  @foreach (var item in Model)
  {
  &lt;p&gt;@Html.DisplayFor(modelItem =&gt; item.Naslov)&lt;/p&gt;
  &lt;p&gt;@Html.DisplayFor(modelItem =&gt; item.Sadrzaj)&lt;/p&gt;
  &lt;p&gt;@Html.DisplayFor(modelItem =&gt; item.Datum)&lt;/p&gt;
  &lt;hr&gt;
  }
  </code></pre>
  
  <p>I get this</p>
  
  <pre><code> Object reference not set to an instance of an object.
  
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
  Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
  
  Source Error: 
  
  
  Line 48:       
  Line 49: 
  Line 50: @foreach (var item in Model)
  Line 51:     {
  Line 52:     &lt;p&gt;@Html.DisplayFor(modelItem =&gt; item.Naslov)&lt;/p&gt;
  </code></pre>
  
  <p>It says the problem is in foreach loop. What I am doing wrong? I just want the same data from one database displayed on two different pages.</p>
  
  <p>Edit: There is also something about NullReferenceException that was unhandled by the user code. Hope this is of any use for you.</p>