C# ASP MVC4每个帖子都有多条评论
基本上,我想实现每个帖子都可以发布多条评论的功能。但是,当用户在第一篇文章上发表评论时,它甚至没有将数据提交给我的控制器,因此我无法获取评论数据,但是如果用户在第二篇文章上发表评论,我可以获取评论数据。为什么? 我的主要观点:C# ASP MVC4每个帖子都有多条评论,c#,asp.net-mvc,C#,Asp.net Mvc,基本上,我想实现每个帖子都可以发布多条评论的功能。但是,当用户在第一篇文章上发表评论时,它甚至没有将数据提交给我的控制器,因此我无法获取评论数据,但是如果用户在第二篇文章上发表评论,我可以获取评论数据。为什么? 我的主要观点: { using (Html.BeginForm("ListPost", "Post", FormMethod.Get, new { @id = "forms" })) {
{
using (Html.BeginForm("ListPost", "Post", FormMethod.Get, new { @id = "forms" }))
{
foreach (var item in Model.listPost)
{
@Html.Partial("~/Views/Shared/PartialView/ListPost.cshtml", new PostModel(item))
}
}
}
我的部分观点:
{
@model PostModel
@using (Html.BeginForm("AddComment", "Comment", FormMethod.Post, new { @id = "forms" + Model.post.PostId }))
{
@Html.HiddenFor(x => x.post.PostId)
@Model.post.PostMessage
<br />
@Html.Image(@Model.post.Member.Avatar.AvatarPath, null, "displayAvatar")
<br />
@Html.Timeago(@Model.post.PostedDate);
<br />
@Html.Label("Comment:")
@Html.TextAreaFor(x => x.postComment.Message, new { @id = Model.post.PostId })
<br />
<input id= "@Model.post.PostId" class="btn btn-default" type="submit" value="Post" />
}
}
{
@模型后模型
@使用(Html.BeginForm(“AddComment”,“Comment”,FormMethod.Post,new{@id=“forms”+Model.Post.PostId}))
{
@Html.HiddenFor(x=>x.post.PostId)
@Model.post.PostMessage
@Html.Image(@Model.post.Member.Avatar.AvatarPath,null,“displayAvatar”)
@Html.Timeago(@Model.post.postedate);
@Label(“注释:”)
@TextAreaFor(x=>x.postComment.Message,新的{@id=Model.post.PostId})
}
}
切勿多次呈现HTML表单以供评论
呈现一次注释表单并将其移动到内部注释,并使用存储父注释id的隐藏字段。不在同一页面上反复呈现表单将节省大量资源
使用JavaScript将表单移动到子级。我以前做过,它按预期运行。这是在创建无效的嵌套表单。摆脱主视图中的外部形态先生~你刚刚救了我的命=D非常感谢你!!!我花了几个小时试图解决这个错误,但每个注释都有一个表单是没有问题的,只要它们不是嵌套的