Close the modal window containing the ASP AJAX ASP form

in webapp I use ASP AJAX form ASP in a modal window. I do not use any specific jQuery code, only some to open a modal window (i.e. showModal () function):

@Ajax.ActionLink("Open", "Add", "Home", new {id = Model.Id}, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "modal", OnSuccess = "showModal()"})

This code loads my form (partial view) into a div and opens it as a modal window. In the submit ActionResult form, I simply use the default ModelState to validate it, and in case of an error, I return the same partial view containing the model errors. This works fine, except in the following situation: when the model contains no errors, I want to automatically close the mod window. I tried the following:

@using (Ajax.BeginForm("Save", "Home", new AjaxOptions {HttpMethod = "POST", UpdateTargetId = "modal", OnSuccess = "hideModal(); alert('Saved');"}))

However, when the model contains errors, the Ajax call is still valid, so OnSuccess is called. I tried to solve this problem by sending an HttpStatusCode error message in a partial view, but then the div is not updated with the new html. I think the only solution is to send a partial view containing javascript that closes the modal window when the model contains no errors, but this solution is not very neat in my opinion. Any other ideas?

+5
source share
1 answer

. , , , JsonResult , true, . OnSuccess AjaxOptions .

[HttpPost]
public ActionResult Hold(JobStatusNoteViewModel model)
{
    if (ModelState.IsValid)
    {
        //do work
        return Json(new {success = true});
    }

    return PartialView("JobStatusNote", model);
 }

PartialView

<% using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "JobStatusForm", OnSuccess = "closePopUp" })) { %>
<div id="JobStatusForm">
    <!-- Form -->
</div>
<% } %>  

<script>
function closePopUp(data) {
 if (data.success) {
     //close popup
   }
}
</script>
+9

All Articles