I have the following modal to add comments to a blog but when I submit the form instead of updating the target id with list of all added comments it redirect to a new page with list of comments? How Do I update the target ID so that it display the new comment along with all the others?
<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);
}
}