1 Replies - 474 Views - Last Post: 07 June 2019 - 04:53 AM Rate Topic: -----

#1 teahou   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 225
  • Joined: 01-April 11

Bootstrap modal not closing after MVC Ajax form submitted

Posted 06 June 2019 - 08:01 AM

I open the modal by adding data- to the calling button:

<a href="@Url.Action("Edit", "Moderate", new {id = Model.SubmissionId})"
 class="btn btn-dark modal-link">Edit</a>




which calls this function to add the data- and open the modal:

$('body').on('click', '.modal-link', function (e) {
    //e.preventDefault();
    $(this).attr('data-target', "#modal-container");
    $(this).attr('data-toggle', 'modal');
});

// boostrap 4 load modal example from docs
$('#modal-container').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); 
    var url = button.attr("href");
    var modal = $(this);
    modal.find('.modal-content').load(url);
});




In the modal is the Ajax form:

    @using (Ajax.BeginForm("Edit", "Moderate",
        new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "formResult", OnSuccess = "OnModalSuccess" }, new { @id = "editSuggestionForm" }))
    {
       form stuff
    }




The controller does a redirect on successful post:

  return RedirectToAction("Index", "Home");




But because the modal wont close, the Index view is opened in the modal.

I have tried these various ways to close:

function OnModalSuccess() {
    $("#modal-close-btn").trigger('click');
}

function OnModalSuccess() {
    $('#modal-container').modal('hide');
}

function OnModalSuccess() {
    $('#modal-container').modal('toggle');
}



Is This A Good Question/Topic? 0
  • +

Replies To: Bootstrap modal not closing after MVC Ajax form submitted

#2 DarenR   User is offline

  • D.I.C Lover

Reputation: 624
  • View blog
  • Posts: 4,169
  • Joined: 12-January 10

Re: Bootstrap modal not closing after MVC Ajax form submitted

Posted 07 June 2019 - 04:53 AM

here is a use of the modal:
My link
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1