问题描述
我只喜欢在用户点击 Button type="submit" id="PostButton" 后更新 @Html.Partial("_CommentSection")。
有没有办法做到这一点,你可以帮助我更多地实现我的目标。 也许用jquery - 但我之前没有用过这个,不知道怎么做
我在 asp.net 项目中有此代码的部分视图:
@model MyProject.Models.Home
@foreach (var comment in MyProject.Controllers.HomeController.GetAllComments(Model.Id))
{
<br />
<div>
<div>
<a>
@MyProject.Controllers.HomeController.GetUserName(comment.UserId)
</a>
added a comment -
<span>
<time>@comment.CommentCreated.ToString()</time>
</span>
<span id="edit-delete-comment">
@using (Html.BeginForm("DeleteComment","Home"))
{
@Html.AntiForgeryToken()
if (MyProject.Controllers.HomeController.CheckIfUserIsCreator(User.Identity.Name,comment.CommentId))
{
if (!MyProject.Controllers.HomeController.CheckDateOfComment(comment.CommentId))
{
<div class="form-actions no-color">
@Html.Hidden("returnUrl",this.Request.RawUrl)
@Html.Hidden("CommentId",comment.CommentId)
<input type="submit" value="Delete" class="t" />
</div>
}
}
}
</span>
</div>
<br />
<div>
<p>
@comment.CommentText
</p>
</div>
</div>
<hr />
}
@using (Html.BeginForm("CreateComment","Home",FormMethod.Post,null))
{
@Html.AntiForgeryToken()
<div class="mt-5 d-flex flex-row">
@Html.Hidden("returnUrl",this.Request.RawUrl)
@Html.Hidden("HomeId",Model.Id)
<textarea class="form-control" name="CommentText"></textarea>
<button class="btn btn-secondary btn-block mt-2 post-btn" type="submit" id="PostButton">Post</button>
</div>
}
我的观点:
<div id="partial-update">
@Html.Partial("_CommentSection")
</div>
我的控制器(帖子被注释掉后更新页面)
[HttpPost]
[AllowOnlyOneRequest]
[ValidateAntiForgeryToken]
public void CreateComment([Bind(Include = "CommentId,CommentText,CommentCreated,UserId,HomeId")] Comment comment,string returnUrl,int HomeId)
{
if (ModelState.IsValid)
{
comment.CommentCreated = DateTime.Now;
comment.UserId = db.Users.FirstOrDefault(i => i.UserEmail == User.Identity.Name).UserId;
comment.HomeId = HomeId;
db.Comments.Add(comment);
db.SaveChanges();
//return Redirect(returnUrl);
}
//return Redirect(returnUrl);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)