Ajax 模态不更新目标 ID
Ajax Modal Not Updating Target ID
我有以下模式向博客添加评论,但是当我提交表单而不是使用所有添加的评论列表更新目标 id 时,它会重定向到带有评论列表的新页面?如何更新目标 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);
}
}
RedirectToAction
将触发客户端重定向。
将返回更改为此,只需调用返回PartialViewResult
的现有方法:
if (Request.IsAjaxRequest())
{
return Comments(id);
}
更新
另请参阅下面的评论以了解另一个方面,即不显眼的 ajax 和 jquery 验证更新。
相关文章:
- Href:当前DIV中的目标ID
- 目标 ID 而不是
- 引导模式的目标 id,用于在模式关闭时停止播放视频
- 对于 JQuery 中具有不同事件的多个目标 ID 的相同函数
- 具有 ID AND 类的 jQuery 选择器目标元素不起作用
- 铬错误或编码错误?目标页面中的锚/ID 链接不起作用
- 具有不同 id 的目标元素 Jquery
- jQuery一次触发所有模态窗口,即使具有不同的目标 #id
- 目标类名,但使用唯一ID调用函数
- 获取已删除的元素id,而不是删除目标id
- 以动态id为目标's在javascript文件中
- 如何在ID更改时使用硒作为输入框的目标
- 只替换目标ID's中的字符串
- 如何动态设置数据目标id
- 如何警告多组ul列表元素的目标id
- 多个目标Id's和ModalPopUp扩展器
- 如何添加一个可拖放的目标id到if语句来检查正确的答案
- 获取动态文本字段的目标id
- Ajax 模态不更新目标 ID
- 显示具有事件目标 ID 的块