title: Display ICollection in View
body: <p>I'm developing a project on ASP.NET MVC5 and using EF6.
  I have the following classes, with a <strong>one-to-many relationship</strong> between Author and Book, where an Author can have multiple Books and a Book can only belong to one Author:</p>
  
  <p><strong>Models:</strong></p>
  
  <pre><code>public class Author 
  {
     public int AuthorId { get; set; }
     public string Name { get; set; }
  
     public ICollection&lt;Book&gt; Books { get; set; }
  }
  
  public class Book 
  {
     public int BookId { get; set; }
     public string Name { get; set; }
  
     public int? AuthorId { get; set; }
     public Author Author { get; set; }
  }
  </code></pre>
  
  <p>I have generated the approppiate controller with views using EF and now, I want to display for each Author the corresponding associated books' names, in Details.cshtml view.</p>
  
  <p>The <strong>Controller</strong>:</p>
  
  <pre><code>// GET: Authors/Details/5
  public async Task&lt;ActionResult&gt; Details(int? id)
  {
      if (id == null)
      {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
  
      Author author = await db.Authors.FindAsync(id);
  
      if (author == null)
      {
         return HttpNotFound();
      }
         return View(author);
  }
  </code></pre>
  
  <p>My <strong>Details.cshtml</strong> as follows:</p>
  
  <pre><code>@model MyApp.Models.Author
  
  &lt;div&gt;
      &lt;h4&gt;Author Info&lt;/h4&gt;
      &lt;hr /&gt;
      &lt;dl class="dl-horizontal"&gt;
          &lt;dt style="font-size:20px"&gt;
              @Html.DisplayNameFor(model =&gt; model.Name)
          &lt;/dt&gt;
  
          &lt;dd style="font-size:20px"&gt;
              @Html.DisplayFor(model =&gt; model.Name)
          &lt;/dd&gt;
  
          &lt;dt style="font-size:20px"&gt;
              Associated Books
          &lt;/dt&gt;
          @foreach (var item in Model.Books)
          {
              &lt;dd&gt;
                  @item.Name
              &lt;/dd&gt;
          }
  
      &lt;/dl&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>But this generates the following error:</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object. Exception
    Details: System.NullReferenceException: Object reference not set to an
    instance of an object.</p>
  </blockquote>
  
  <p>I have followed the answer to this similar question (<a href="https://stackoverflow.com/questions/24494718/how-to-display-a-collection-in-view-of-asp-net-mvc-razor-project">How to display a collection in View of ASP.NET MVC Razor project?</a>) but, obviously, with no success. Can anyone tell me what am I doing wrong?</p>