在 ASP.NET MVC 中向 post 添加注释
Adding comment to post in ASP.NET MVC
编辑:我试图在我的 mvc 应用程序中向 post 添加评论,但我的操作似乎不起作用。当我到达 Action 时,我想传递来自 Post 的 Id(来自 table/model FormalBlog 的 Id)但是 newPost.Post = model.Post 为 null 并且当操作到达 db.SaveChanges 它抛出一个 System.Data.Entity.Validation.DbEntityValidationException。
下面是我的操作和我的 PostIndexViewModel:
public ActionResult Comment(PostIndexViewModel model, FormalBlog Post)
{
var userName = User.Identity.Name;
var author = db.Users.SingleOrDefault(x => x.UserName == userName);
Comment newPost = new Comment();
newPost.Author = author;
newPost.Text = model.Text;
newPost.Post = model.Post;
db.Comments.Add(newPost);
db.SaveChanges();
return RedirectToAction("ShowBlogs", "Blog");
}
}
public class PostIndexViewModel
{
public string Id { get; set; }
public ICollection<FormalBlog> FormalBlogs { get; set; }
public FormalBlog NewFormalBlog { get; set; } = new FormalBlog();
public Category NewCategory { get; set; } = new Category();
public ICollection<Category> Categories { get; set; }
public List<SelectListItem> SelectedCategories { get; set; }
public int[] CategoryIds { get; set; }
public Category CategoryN { get; set; }
public ICollection<Meeting> Meetings { get; set; } //testrad
// public int Id { get; set; }
public string Text { get; set; }
public ApplicationUser Author { get; set; }
public Comment NewComment { get; set; }
public FormalBlog Post { get; set; }
}
这是我的观点的代码:
@model XP_Scrum_Grupp2.Controllers.PostIndexViewModel
@using (Html.BeginForm("Comment", "Blog", new { formal = Model }, FormMethod.Post, new { id = Model.Id }))
{
<div class="comment-form-container">
<form class="comment-form" data-action="@Url.Action("Comment", "Blog")">
@Html.HiddenFor(m => m.Id)
<div>@Html.DisplayFor(m => m.Author)</div>
<div>
<div>@Html.LabelFor(m => m.Text)</div>
@Html.TextAreaFor(m => m.Text, new { Class = "comment-text", rows = "3", cols = "50" })
</div>
<div class="comment-result" style="display: none;">
<span class="comment-result-text">An error occurred</span>
</div>
<div>
<button type="submit" class="comment-form-submit">Submit comment</button>
</div>
</form>
</div>
}
您没有将任何数据作为 model.NewComment.Text
发布,因此发生错误是因为 NewComment
对象是 null
。
@Html.TextAreaFor(m => m.Text, new { Class = "comment-text", rows = "3", cols = "50" })
所以,试着改变它;
newPost.Text = model.NewComment.Text;
至
newPost.Text = model.Text;
您的观点没有为 model.NewComment
赋予任何价值。因此,当您访问 model.NewComment.Text
时,它将抛出 Null 引用异常,因为 model.NewComment
为空。
您将新文本分配给模型的测试 属性。所以你应该使用 model.Text
而不是 model.NewComment.Text
public ActionResult Comment(PostIndexViewModel model)
{
var userName = User.Identity.Name;
var author = db.Users.SingleOrDefault(x => x.UserName == userName);
Comment newPost = new Comment();
newPost.Author = author;
newPost.Text = model.Text;
newPost.Post = model.Post;
db.Comments.Add(newPost);
db.SaveChanges();
return RedirectToAction("ShowBlogs", "Blog");
}
编辑:我试图在我的 mvc 应用程序中向 post 添加评论,但我的操作似乎不起作用。当我到达 Action 时,我想传递来自 Post 的 Id(来自 table/model FormalBlog 的 Id)但是 newPost.Post = model.Post 为 null 并且当操作到达 db.SaveChanges 它抛出一个 System.Data.Entity.Validation.DbEntityValidationException。
下面是我的操作和我的 PostIndexViewModel:
public ActionResult Comment(PostIndexViewModel model, FormalBlog Post)
{
var userName = User.Identity.Name;
var author = db.Users.SingleOrDefault(x => x.UserName == userName);
Comment newPost = new Comment();
newPost.Author = author;
newPost.Text = model.Text;
newPost.Post = model.Post;
db.Comments.Add(newPost);
db.SaveChanges();
return RedirectToAction("ShowBlogs", "Blog");
}
}
public class PostIndexViewModel
{
public string Id { get; set; }
public ICollection<FormalBlog> FormalBlogs { get; set; }
public FormalBlog NewFormalBlog { get; set; } = new FormalBlog();
public Category NewCategory { get; set; } = new Category();
public ICollection<Category> Categories { get; set; }
public List<SelectListItem> SelectedCategories { get; set; }
public int[] CategoryIds { get; set; }
public Category CategoryN { get; set; }
public ICollection<Meeting> Meetings { get; set; } //testrad
// public int Id { get; set; }
public string Text { get; set; }
public ApplicationUser Author { get; set; }
public Comment NewComment { get; set; }
public FormalBlog Post { get; set; }
}
这是我的观点的代码:
@model XP_Scrum_Grupp2.Controllers.PostIndexViewModel
@using (Html.BeginForm("Comment", "Blog", new { formal = Model }, FormMethod.Post, new { id = Model.Id }))
{
<div class="comment-form-container">
<form class="comment-form" data-action="@Url.Action("Comment", "Blog")">
@Html.HiddenFor(m => m.Id)
<div>@Html.DisplayFor(m => m.Author)</div>
<div>
<div>@Html.LabelFor(m => m.Text)</div>
@Html.TextAreaFor(m => m.Text, new { Class = "comment-text", rows = "3", cols = "50" })
</div>
<div class="comment-result" style="display: none;">
<span class="comment-result-text">An error occurred</span>
</div>
<div>
<button type="submit" class="comment-form-submit">Submit comment</button>
</div>
</form>
</div>
}
您没有将任何数据作为 model.NewComment.Text
发布,因此发生错误是因为 NewComment
对象是 null
。
@Html.TextAreaFor(m => m.Text, new { Class = "comment-text", rows = "3", cols = "50" })
所以,试着改变它;
newPost.Text = model.NewComment.Text;
至
newPost.Text = model.Text;
您的观点没有为 model.NewComment
赋予任何价值。因此,当您访问 model.NewComment.Text
时,它将抛出 Null 引用异常,因为 model.NewComment
为空。
您将新文本分配给模型的测试 属性。所以你应该使用 model.Text
而不是 model.NewComment.Text
public ActionResult Comment(PostIndexViewModel model)
{
var userName = User.Identity.Name;
var author = db.Users.SingleOrDefault(x => x.UserName == userName);
Comment newPost = new Comment();
newPost.Author = author;
newPost.Text = model.Text;
newPost.Post = model.Post;
db.Comments.Add(newPost);
db.SaveChanges();
return RedirectToAction("ShowBlogs", "Blog");
}