Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing Bootstrap Modal form after a Successful Ajax Request

My modal form successfully makes an Ajax request. The data received is displayed in the background. Invoking the modal is done by data-* attributes as bootstrap examples show here. But the modal is not getting dismissed. I tried to add

OnSuccess = "$('#searchForm').modal('hide');"

to my Ajax.BeginForm. But this doesn't remove the fade effect that modal cast on the background.

My View is:

<div class="modal fade" id="searchForm" tabindex="-1" role="dialog" aria-labelledby="searchFormLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="searchFormLabel">Search Here</h4>
            </div>
            <div class="modal-body">
                @using (Ajax.BeginForm(new AjaxOptions
                {
                    HttpMethod = "GET",
                    InsertionMode = InsertionMode.Replace,
                    UpdateTargetId = "results",
                    OnSuccess = "$('#searchForm').modal('hide');"
                }))
                {
                    // input fields and submit button are here
                }
            </div>

           </div>
    </div>
</div>

Am I missing something?

like image 499
Vivekanand P V Avatar asked Dec 29 '15 05:12

Vivekanand P V


People also ask

How do you close modal after submit in Ajax?

To close bootstrap modal you can pass 'hide' as option to modal method as follows. $('#CompanyProfile'). modal('hide'); Use this code inside ajax success.

How do I close a bootstrap modal?

Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.

How do you force close modals?

Answer: Use the modal('hide') Method You can simply use the modal('hide') method to hide or close the modal window in Bootstrap using jQuery. Other related Bootstrap's modal methods are modal('show') and modal('toggle') .

How do I stop a bootstrap modal from closing?

When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.


1 Answers

In this case, you are opening up the modal via data-attributes, and then closing it using jQuery. So, in a way both modal toggling methods are used, which are conflicting with each other. So, remove the data-attributes and show/hide the modal using jQuery only.

Try this:

$('#searchForm').modal('show'); //for showing the modal

For hiding on OnSuccess:

OnSuccess = "unloadModal"

Function unloadModal will be:

function unloadModal() {
    $('#searchForm').modal('hide');
};
like image 142
Senjuti Mahapatra Avatar answered Oct 04 '22 00:10

Senjuti Mahapatra