【发布时间】:2021-04-17 20:34:55
【问题描述】:
我只喜欢在用户单击 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);
}
【问题讨论】:
标签: jquery asp.net asp.net-mvc partial-views