title: How do I pass this child model reference to a virtual ICollection in parent domain model in ASP.NET MVC?
body: <p>I'm creating a commenting system for my ASP.NET MVC blog engine using a view form that triggers a basic controller action method:</p>
  
  <p><strong>FORM:</strong></p>
  
  <pre><code> @if (User.Identity.IsAuthenticated)
      {
          //using (Html.BeginForm())
          //  {
          &lt;div class="new_comment"&gt;
              &lt;h6 id="shortcodes" class="page-header"&gt;&lt;i class="fa fa-file-text-o"&gt;&lt;/i&gt; Leave a comment&lt;/h6&gt;
              &lt;div class="hline"&gt;&lt;/div&gt;
              &lt;form class="form-horizontal" action="@Url.Action("CreateComment")" method="post" role="form"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;div class="col-sm-4 col-md-4"&gt;
                          &lt;textarea rows="7" class="form-control" name="Message" placeholder="Your Comment Here..."&gt;&lt;/textarea&gt;
                          @Html.AntiForgeryToken()
                          &lt;input type="hidden" name="Slug" value="@Model.Slug"/&gt;
                          &lt;input type="hidden" name="PostId" value="@Model.Id"/&gt;
                          &lt;br/&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;input type="submit" value="Post Comment" class="btn btn-primary" style="margin-left: 12px"/&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
          //}
      }
  </code></pre>
  
  <p><strong>CONTROLLER:</strong></p>
  
  <pre><code>    [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult CreateComment([Bind(Include = "PostId,Message,Username,DatePosted")]Comment comment)
      {
          var post = db.BlogPosts.Find(comment.PostId);
          if (post == null)
              return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  
          if (ModelState.IsValid)
          {
              comment.Username = User.Identity.GetUserId();
              comment.DatePosted = DateTimeOffset.Now;
              db.Comments.Add(comment);
              db.SaveChanges();
          }
  
          return RedirectToAction("BlogPostDetails", new { slug = post.Slug});
      }
  </code></pre>
  
  <p>I've set breakpoints aside each of the expressions contained within the if statement and confirmed that none of the data values being passed <code>("PostId, Message, Username, DatePosted")</code> are null, and that <code>db.SaveChances()</code> is commiting changes to the database. Next, here is<code>Models.BlogPosts</code>...</p>
  
  <p><strong>MODELS:</strong></p>
  
  <pre><code> public class BlogPosts
  {
      public BlogPosts()
      {
          this.Comments = new HashSet&lt;Comment&gt;();
      }
  
      public int Id { get; set; }
      public DateTimeOffset Created { get; set; }
      public DateTimeOffset? Updated { get; set; }
      [AllowHtml]
      [Required]
      public string Title { get; set; }
      public string Slug { get; set; }
      [Required]
      public string Category { get; set; }
      [AllowHtml]
      [Required]
      public string Body { get; set; }
      public string MediaURL { get; set; }
      public bool Published { get; set; }
      public virtual ICollection&lt;Comment&gt; Comments { get; set; }
  }
  
  public class Comment
  {
      public int Id { get; set; }
      public int PostId { get; set; }
      public string Username { get; set; }
  
      [Required]
      [DataType(DataType.MultilineText)]
      public string Message { get; set; }
      public DateTimeOffset DatePosted { get; set; }
      public Nullable&lt;System.DateTimeOffset&gt; Edited { get; set; }
  
      public virtual BlogPosts BlogPost { get; set; }
      public virtual ApplicationUser Author { get; set; }
      //public int? ParentId { get; set; }
      //[ForeignKey("ParentId")]
      //public virtual ICollection&lt;Comment&gt; Children { get; set; }
      //public string ParentComment { get; internal set; }
  }
  </code></pre>
  
  <p>And here is the view that fails to execute:</p>
  
  <p><strong>VIEW THAT DOES NOT EXECUTE</strong></p>
  
  <pre><code> @foreach (var item in Model.Comments.OrderByDescending(c =&gt; c.DatePosted))
      {
          &lt;div class="comment"&gt;
              &lt;p&gt;
                  @if (item.Username == null)
                  {
                      &lt;small&gt;By: Anonymous&lt;/small&gt;&lt;span&gt;|&lt;/span&gt;
                  }
                  else
                  {
                      &lt;small&gt;By: @Html.DisplayFor(modelItem =&gt; item.Username)&lt;/small&gt;&lt;span&gt;|&lt;/span&gt;
                  }
                  &lt;small&gt;Date: @Html.DisplayFor(modelItem =&gt; item.DatePosted)&lt;/small&gt;
                  @if (item.Edited != null)
                  {
                      &lt;span&gt;|&lt;/span&gt;&lt;small&gt;Updated: @Html.DisplayFor(modelItem =&gt; item.Edited)&lt;/small&gt;
                  }
              &lt;/p&gt;
              &lt;div&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Message)
              &lt;/div&gt;
          &lt;/div&gt;
          if (item.Username == User.Identity.GetUserId() || User.IsInRole("Admin") || User.IsInRole("Moderator"))
          {
              &lt;div&gt;
                  @Html.ActionLink("Edit", "_EditComment", new { id = item.Id }) &lt;span&gt;|&lt;/span&gt;
                  @Html.ActionLink("Delete", "_DeleteComment", new { id = item.Id })
              &lt;/div&gt;
          }
          &lt;br /&gt;
          &lt;!--&lt;div class="hline"&gt;&lt;/div&gt;--&gt;
      }
      &lt;div&gt;
          &lt;input type="button" class="btn btn-primary" value="Return to Blog Roll" onclick="location.href = '@Url.Action("BlogIndex")'"&gt;
      &lt;/div&gt;
      &lt;br /&gt;
      @if (User.Identity.IsAuthenticated || User.IsInRole("Admin") || User.IsInRole("Moderator"))
      {
          &lt;input type="button" class="btn btn-primary" value="Modify Post" onclick="location.href = '@Url.Action("BlogAdmin")'"&gt;
          &lt;br /&gt;
          &lt;br /&gt;
      }
  </code></pre>
  
  <p>When setting a breakpoint on the first line in the view above: <code>@foreach (var item in Model.Comments.OrderByDescending(c =&gt; c.DatePosted))</code>, the reference to
  <code>public virtual ICollection&lt;Comment&gt; Comments</code> within the <code>Models.BlogPosts</code> class remains null (which obviously means the logic in my view fails to execute and no comment is posted).</p>
  
  <p>I am new to ASP.NET MVC, EF Code-First, etc., and clearly do not understand how my controller is failing to pass the comment values in the child model to the <code>public virtual ICollection&lt;Comment&gt; Comments</code> in the parent... How is it that <code>Models.Comment</code> as referenced in my <code>CommentCreate</code> controller contains a value, and the very same virtual reference in my <code>Models.BlogPosts</code> does not?</p>
  
  <p><strong>EDIT:</strong> After great feedback from several users on both the cosmetic errors and critical errors in my code, as well as helpful ASP.NET MVC resource pointers, I determined that the null references being passed had to do with incorrect property-naming conventions in my domain models. See answer below.</p>