의견이있는 뉴스로 사이트를 만들려고합니다. 나는 pluralsight.com에서 가이드를 따라 왔으며 편지의 가이드에서 수행 된 작업을 수행했지만 런타임시 모델 내부의 내용을 디버깅하고 볼 때 주석이 포함되지 않습니다.ICollection의 객체가 보이지 않는 모델로 전달되지 않았습니다.
내 DB 클래스 : 참여
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);
}
}
모델 :
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; }
}
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 &&
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>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)
필자는 예제를 살펴본 결과 비디오 가이드를 십여 번 보면서 내가 잘못했는지 확인했습니다. 내가 생각해 낸 유일한 해결책은 NodeContent 테이블을 Comments 테이블에 결합하고 그것을 다른 모델로 투영하는 것이지만, 내가 수집 할 수있는 것은 필요하지 않아야한다.
도움을 주시면 감사하겠습니다.
완벽하게 일했습니다! 도와 주셔서 정말로 고맙습니다. – grimurd