ICollection中的对象不能通过模型传递来查看 [英] Objects in ICollection not passed with model to view
问题描述
我正在尝试创建一个可以发表评论的消息的网站。我一直在从pluralsight.com跟随指导,我已经遵循了这封信的指导,但是当我调试并在运行时看看模型里面的内容时,不会包含评论。
我的DB类:
public class ProjectDB {
public DbSet< ContentNode> ContentNodes {get;组; }
public DbSet< Comment>评论{get;组;
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);
}
}
涉及的模型:
public class ContentNode:Node
{
public ContentType ContentType {get;组; }
public ContentCategory类别{get;组; }
public ICollection< Comment>评论{get;组; }
public ContentNode()
{
注释=新列表<注释>();
}
}
public class注释
{
public int ID {get;组; }
public ContentNode Node {get;组; }
public string Body {get;组; }
}
控制器方法从db中提取新闻文章并将其发送到视图
[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&&b $ b f.dateCreated.Month == month&&
f.dateCreated.Day == day&&
f.Title == title& &
f.ContentType.ID == 1
select f).Single();
return View(model);
}
最后视图本身:
@model Project.Models.ContentNode
@ {
ViewBag.Title =GetByID;
}
< h2> GetByID< / h2>
< fieldset>
< legend>新闻< / 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(编辑,编辑,新的{id = Model.ID})|
@ Html.ActionLink(返回列表,索引)
< / p>
@foreach(Model.Comments中的var项)
{
@ Html.Partial(_评论,项目)
}
@ Html.ActionLink(新评论,创建,评论,新的{NodeID = Model.ID},null)
我已经看了一些例子,我看了几十次视频指南,看看我做错了什么。我想出的唯一的解决方案是加入NodeContent表与注释表,并将其投射到另一个模型,但从我可以收集不应该需要。
var model = _db.ContentNodes
.Include(f => f.Comments)// eager加载子集
.Single(f => f.dateCreated.Year == year
&& f.dateCreated .Month == month
&& f.dateCreated.Day == day
&& f.Title == title
&& f.ContentType.Id == 1);
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.
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);
这篇关于ICollection中的对象不能通过模型传递来查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!