我有问题让我的博客创建评论工作,当我在db中编辑关系id时,它正常工作但我在使用表单时无法得到它。
错误在于:Blogposts = id
public ActionResult BlogPost(int Id)
{
var _getSpecificBlogPost = db.Blogposts.Where(m => m.Id == Id).ToList();
return View(_getSpecificBlogPost);
}
[HttpPost]
public ActionResult BlogComment_Create(string name, string bodytext, string id )
{
BlogComment model = new BlogComment { Name = name, BodyText = bodytext, Blogposts = id};
db.BlogComments.Add(model);
db.SaveChanges();
return Redirect(Request.UrlReferrer.PathAndQuery);
}
在BlogComment
我有:public virtual Blogpost Blogposts { get; set; }
在BlogPosts
我有:public virtual ICollection<BlogComment> BlogComments { get; set; }
<form method="post" id="form-variant-create" action="@Url.Action("BlogComment_Create", "Blog")" role="form">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" />
<br />
<textarea name="bodytext" class="form-control" rows="3" placeholder="Message"></textarea>
<input type="text" class="hidden" name="id" value="@foreach(var item in Model){@item.Id}" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
答案 0 :(得分:0)
BlogPosts来自BlogPost类型。您需要为BlogPostId创建一个属性作为ForeignKey。
public virtual int BlogPostId{get;set;}
将此行插入CommentModel。然后转而使用BlogPosts。
并编辑您的BlogPosts属性。
[ForeignKey("BlogPostId")]
public virtual Blogpost Blogposts { get; set; }