我已经搜索了如何处理,但到目前为止我一无所获。有我的问题,我有一个帖子模型和一个评论模型。我的CreateView有输入来创建帖子,它也显示在这个创建视图中。我们来看看代码:
EDIT2:在我的PostController中添加CreateComment Action Result; &安培;我的部分ListPost视图中的ActionLink
现在我可以看到我添加注释的链接,它在我的CreateComment视图中发送,但是当我尝试将其保存在我的数据库中时,我遇到了这个错误:
INSERT语句与FOREIGN KEY约束冲突 \ “FK_dbo.Comments_dbo.Posts_PostId \”。冲突发生在 database \ Myproject“,table \”dbo.Posts \“,列'PostId'。\ r \ n 声明已被终止。“
PostController
public ActionResult ListPost()
{
var post = db.Posts.ToList();
return PartialView("ListPost", post);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(FormCollection values)
{
var post = new Post();
TryUpdateModel(post);
if (ModelState.IsValid)
{
var context = new UsersContext();
var username = User.Identity.Name;
var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
var userid = user.UserId;
post.UserId = userid;
post.Date = DateTime.Now;
db.Posts.Add(post);
db.SaveChanges();
return RedirectToAction("Create", "Post");
}
return View(post);
}
public ActionResult CreateComment()
{
ViewBag.PostId = new SelectList(db.Posts, "PostId", "Content");
return View("CreateComment");
}
[HttpPost]
public ActionResult CreateComment(FormCollection values)
{
var comment = new Comment();
TryUpdateModel(comment);
if (ModelState.IsValid)
{
var context = new UsersContext();
var username = User.Identity.Name;
var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
var userid = user.UserId;
comment.UserId = userid;
comment.Date = DateTime.Now;
db.Comments.Add(comment);
db.SaveChanges();
return RedirectToAction("Create", "Post");
}
ViewBag.PostId = new SelectList(db.Posts, "PostId", "Content", comment.PostId);
return View(comment);
}
创建视图:
@model Myproject.Models.Post
@using (Html.BeginForm("Create", "Post", FormMethod.Post))
{
<legend>Add Post</legend>
<div class="editor-field">
@Html.EditorFor(model => model.Content)
@Html.ValidationMessageFor(model => model.Content)
<input type="file" name="Photo" id="Photo"/>
</div>
<p>
<input type="submit" value="Post" />
</p>
}
@{Html.RenderAction("ListPost", "Post");}
ListPost视图
@model IEnumerable<MyProject.Models.Post>
@foreach (var item in Model.OrderByDescending(x => x.Date))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Content)
</td>
</tr>
<tr>
<td>
@Html.DisplayFor(modelItem => item.Date)
<span>@Html.DisplayFor(modelItem => item.Users.UserName)</span>
</td>
</tr>
<tr>
<td> @Html.ActionLink("Add Comment", "CreateComment", new {id=item.PostId})</td>
</tr>
}
现在,我想在帖子中添加评论。我的评论模型(和表)有一个postId属性。如何在我的ListPost视图中包含我的createCommentView,我想我必须将它包含在这个视图中而不是我的CreatePostView中,但我也不确定。在这两种情况下,我都不知道该怎么做,因为我在同一视图中使用了注释模型和帖子模型......
任何想法,链接到哪里可以发送给我,或者其他什么? 谢谢,抱歉我的英文不好
答案 0 :(得分:2)
@Html.ActionLink("Add Comment", "CreateComments", new { id=item.PostId })
确保将PostId作为属性传递给您的模型。
答案 1 :(得分:0)
通过Neeraj的回答并添加下面指定的三行,它可以正常工作
[HttpPost]
public ActionResult CreateComment(FormCollection values, int id)
{
var comment = new Comment();
TryUpdateModel(comment);
/** ADD THIS LINE **/
Post post = db.Posts.Find(id);
if (ModelState.IsValid)
{
var context = new UsersContext();
var username = User.Identity.Name;
var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
var userid = user.UserId;
comment.UserId = userid;
comment.Date = DateTime.Now;
/*** ADD THIS TWO LINES ***/
comment.Post = post;
comment.PostId = post.PostId;
db.Comments.Add(comment);
db.SaveChanges();
return RedirectToAction("Create", "Post");
}
ViewBag.PostId = new SelectList(db.Posts, "PostId", "Content", comment.PostId);
return View(comment);
}
答案 2 :(得分:0)
createCommentView应该是部分视图,它将在ListPost视图的html下面使用,到这个局部视图传递post id。在局部视图中,在Html.BeginForm中取一个文本框进行注释(&#34; Comment&#34;,& #34; CreateComment&#34;,FormMethod.Post)并将评论传递给新动作CreateComment
局部视图shd就像
@model Comment //comment class has post id and comment text
@using (Html.BeginForm("Comment", "CreateComment", FormMethod.Post))
{
<legend>Add Post</legend>
<div class="editor-field">
@Html.EditorFor(model => model.Comment)
@Html.EditorFor(model => model.PostId)
</div>
<p>
<input type="submit" value="Post" />
</p>
}
现在在您的列表帖子视图中使用此
@Html.Partial("CommentView", new Comment(){PostId=item.PostId})