title: ASP.NET Core Model null error in the Index view
body: <p>Please see the Error section below. Other related code is as follows:</p>
  
  <p><strong>Model</strong>:</p>
  
  <pre><code>public class Blog
      {
          public int BlogId { get; set; }
          public string Url { get; set; }
  
          public List&lt;Post&gt; Posts { get; set; }
      }
  
      public class Post
      {
          public int PostId { get; set; }
          public string Title { get; set; }
          public string Content { get; set; }
  
          public int BlogId { get; set; }
          public Blog Blog { get; set; }
      }
  </code></pre>
  
  <p><strong>BlogPostController</strong></p>
  
  <pre><code>public IActionResult Index()
          {
              return View("~/Views/BlogsTest/Index.cshtml");
          }
  </code></pre>
  
  <p><strong>View</strong> [Index.cshtml]:</p>
  
  <pre><code>@model IEnumerable&lt;ASPCoreCodeFirstApp.Models.Blog&gt;
  
  &lt;p&gt;
      &lt;a asp-controller="BlogPost" asp-action="Create"&gt;Create New&lt;/a&gt;
  &lt;/p&gt;
  &lt;table class="table"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.Url)
              &lt;/th&gt;
              &lt;th&gt;&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          @foreach (var item in Model)
          {
              &lt;tr&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Url)
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;a asp-controller="BlogPost" asp-action="Edit" asp-route-id="@item.BlogId"&gt;Edit&lt;/a&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;a asp-controller="BlogPost" asp-action="Create" asp-route-id="@item.BlogId"&gt;Edit&lt;/a&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          }
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>Error at</strong> <code>@foreach (var item in Model)....line</code>:</p>
  
  <p>I can see the Index method gets called and when I put the breakpoint at the <code>@foreach (var item in Model)</code> line of Index.cshtml file I get Model as null and the browser throws the following error. Blogs table does have data. Even if it did not have data the foreach loop should have returned empty result instead of an NULL exception error. The view relative path in the Index action method seems write since Index.cshtml view does get called. What I may be missing?:</p>
  
  <pre><code>NullReferenceException: Object reference not set to an instance of an object.
  MoveNext in Index.cshtml, line 22
  </code></pre>
  
  <p><strong>Folder Hierarchy</strong>:</p>
  
  <p>Model above is in Model.cs file highlighted below:</p>
  
  <p><a href="https://i.stack.imgur.com/YSUkK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YSUkK.png" alt="enter image description here"></a></p>