【发布时间】:2015-06-19 10:09:03
【问题描述】:
我有以下模式可以将 cmets 添加到博客,但是当我提交表单而不是使用所有已添加 cmets 的列表更新目标 ID 时,它会重定向到带有 cmets 列表的新页面?如何更新目标 ID 以便它与所有其他评论一起显示新评论?
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal"> Launch demo modal</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
@using (Ajax.BeginForm("AddComment", "Blog", new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "comments",
LoadingElementId = "progress",
OnSuccess = "$('#myModal').modal('hide');"
}))
{
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">Add Comment</h4>
</div>
<div class="modal-body">
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Blog.BlogID)
<div class="form-group">
@Html.LabelFor(model => model.BlogComment.Comment)
@Html.TextAreaFor(model => model.BlogComment.Comment, 4, 104, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.BlogComment.Comment)
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
}
</div>
</div>
</div>
<div id="comments">
@Html.Action("Comments", "Blog", new { id = Model.Blog.ID })
</div>
public PartialViewResult Comments(int id)
{
IEnumerable<BlogComment> CommentList = _repository.GetBlogComments(id);
return PartialView("_BlogComments", CommentList);
}
public ActionResult AddComment(// All Pramameters)
{
if (ModelState.IsValid)
{
// Do Save Comment
if (Request.IsAjaxRequest())
{
return RedirectToAction("Comments", new { id = id });
}
}
else
{
//return to modal with errors
return PartialView("_CreateComment", BlogViewModel);
}
}
【问题讨论】:
-
与
if (Request.IsAjaxRequest())无关。我可以完全消除这种情况。它仍然没有更新作为操作的 targetid。我认为问题可能是添加评论后如何返回视图。
标签: javascript jquery ajax asp.net-mvc bootstrap-modal