grimurd
grimurd

Reputation: 2850

Objects in ICollection not passed with model to view

I'm trying to create a site with news that can have comments. I've been following guides from pluralsight.com and I've followed what is done in the guide to the letter but when i debug and look at what's inside the model at runtime the comments aren't included.

My DB class:

public class ProjectDB {
        public DbSet<ContentNode> ContentNodes{ get; set; }
        public DbSet<Comment> Comments { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<ContentNode>()
                .Property(n => n.ID).HasColumnName("NodeID");
            modelBuilder.Entity<ContentNode>()
                .HasMany(node => node.Comments)
                .WithRequired(comment => comment.Node);
        }
}

The models involved:

    public class ContentNode : Node
    {
        public ContentType ContentType { get; set; }
        public ContentCategory Category { get; set; }
        public ICollection<Comment> Comments { get; set; }

        public ContentNode()
        {
            Comments = new List<Comment>();
        }
    }

    public class Comment
    {
        public int ID { get; set; }
        public ContentNode Node { get; set; }
        public string Body { get; set; }
    }

The controller method that pulls the news article from the db and sends it to the view

[GET("frettir/{year}/{month}/{day}/{title}")]
public ActionResult GetArticle( int year, int month, int day, string title)
{
    var model = (from f in _db.ContentNodes
                 where f.dateCreated.Year == year &&
                       f.dateCreated.Month == month &&
                       f.dateCreated.Day == day &&
                       f.Title == title &&
                       f.ContentType.ID == 1
                 select f).Single();

    return View(model);
}

And finally the view itself:

@model Project.Models.ContentNode

@{
    ViewBag.Title = "GetByID";
}

<h2>GetByID</h2>

<fieldset>
    <legend>News</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Title)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Title)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Body)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Body)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.dateCreated)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.dateCreated)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

    @foreach (var item in Model.Comments)
    {
        @Html.Partial("_Comment",item)
    }

@Html.ActionLink("New comment", "Create", "Comment", new { NodeID = Model.ID }, null)

I've looked at examples and I've looked over the video guide a dozen times to see what i did wrong. The only solution that I've come up with is joining the NodeContent table with the Comments table and projecting it into another model but from what i can gather that shouldn't be required.

Any help is appreciated.

Upvotes: 0

Views: 262

Answers (2)

danludwig
danludwig

Reputation: 47375

var model = _db.ContentNodes
    .Include(f => f.Comments) // eager load the child collection
    .Single(f => f.dateCreated.Year == year 
         && f.dateCreated.Month == month
         && f.dateCreated.Day == day 
         && f.Title == title 
         && f.ContentType.Id == 1);

Upvotes: 2

rexcfnghk
rexcfnghk

Reputation: 15442

Most likely this is due to the fact that Entity Framework has lazy-loading enabled by default. You can explictly force the query to run by using the First method instead of the Single method:

var model = (from f in _db.ContentNodes
             where f.dateCreated.Year == year &&
                   f.dateCreated.Month == month &&
                   f.dateCreated.Day == day &&
                   f.Title == title &&
                   f.ContentType.ID == 1
             select f).First();
return View(model);

Upvotes: 0

Related Questions