Brendan Vogt
Brendan Vogt

Reputation: 26028

How to close a jQuery dialog after an AJAX JSON call

I am using ASP.NET MVC 4, jQuery, and jQuery UI.

I have a dialog on my view. When I click a button the dialog pops up, takes the values on the dialog and send its through to a service. The service does what it needs to do and will either send back a blank message if it is successful or the actual error message. After this I need to check the error on the client side, close the current dialog and open a success dialog or the error dialog. I'm not sure how to close the current dialog and to display another dialog.

My button:

<button id="TestButton" type="button">Display pop up</button>

My dialogs:

<div id="confirmationDialog"></div>
<div id="successDialog"></div>
<div id="errorDialog">error dialog</div>

my jQuery code:

$('#TestButton').click(function () {
    $('#confirmationDialog').dialog('open');
});

$('#errorDialog').dialog({
    autoOpen: false,
    modal: true,
    resizable: false,
    width: 500,
    title: 'Add Rule Detail Error',
    buttons: {
        'Ok': function () {
            $(this).dialog('close');
        }
    }
});

$('#confirmationDialog').dialog({
    autoOpen: false,
    modal: true,
    resizable: false,
    width: 330,
    title: 'Add Rule Detail Confirmation',
    open: function (event, ui) {
        $(this).load('@Url.Action("AddRuleConfirmation")' +
            '?systemCode=' + $('#SystemCode').val());
        },
        buttons: {
            'Yes': function () {
                var url = '@Url.Action("AddRuleConfirmationSubmit")';
                var data = {
                    systemCode: $('#SystemCode').val()
                };

                $.getJSON(url, data, function (message) {
                    alert(message);
                    if (message == '') {
                        $(this).dialog('close');
                    }
                    else {
                        $(this).dialog('close');
                        $('#errorDialog').dialog('open');
                    }
                });
            },
            'No': function () {
                $(this).dialog('close');
            }
        }
    });

My action methods:

public ActionResult AddRuleConfirmation(string systemCode)
{
    DetailConfirmationViewModel viewModel = new DetailConfirmationViewModel()
    {
        SystemCode = systemCode
    };

    return PartialView("_AddRuleConfirmation", viewModel);
}

public ActionResult AddRuleConfirmationSubmit(string systemCode)
{
    CreateRuleViewModel viewModel = new CreateRuleViewModel()
    {
        SystemCode = systemCode
    };

    ResCodeRuleAdd_Type result = ruleService.AddRule(viewModel);
    string message = string.Empty;

    if (result != ResCodeRuleAdd_Type.R00)
    {
        // Get the error message from resource file
        message = ...
    }

    return Json(message, JsonRequestBehavior.AllowGet);
}

How do I close the current pop up after the get JSON call and open another?

Upvotes: 2

Views: 11061

Answers (3)

David
David

Reputation: 362

var dialogAviso; 

    url = "search.php";
    $.ajax( {
            "type": "POST", 
            "url": url, 
            "data": data, 
            "global": false,
            "async": true,
               "success": function(html){
                    msg_title ="Search";
                    msg_width = "600px";
                    showDialog(html,msg_title,msg_width);
                }
         } );           


function showDialog(texto, titulo, width,height){
.......................
// IMPORTANT: send info to the aux variable, so you can access it from the dialog.
    dialogAviso = $('#divaviso').dialog({
        autoOpen: true,
        width: width,
        height:height,
        modal:true,
        resizable: false,
        title: titulo,
        dialogClass: 'dialog',
        closeOnEscape:true,
        beforeClose: function(){
        },
        close: function(event, ui) { 
            $(this).dialog( "destroy" );            
        },
        show: "slide",
        zindex: 100,
        stack:true,
        buttons: {} 
    }); 
    $('#divaviso').html(texto); 
}


search.php: 
<table>
    <tr>
    <td><a style=\"text-decoration:underline;cursor:pointer;\" onclick="returnInfo(8)">Hello World</td>';
    </tr>   
</table>

functin returnInfo (id){
// Do something with the selected item

// close dialog
dialogAviso.dialog("close");

}

Upvotes: -1

Mark Schultheiss
Mark Schultheiss

Reputation: 34178

You have to add the dialog to the page first: Put this prior to your current:

$('#errorDialog').dialog({
    autoOpen: false,
    modal: true,
    resizable: false,
    width: 330,
    title: 'My Error Dialog'
});
//current code follows:
$('#confirmationDialog').dialog({

Then what you have should work.

EDIT: I thought about this a bit, you probably need to fix the scope of the $(this) inside the success handler.

change to do:

var myDialog = $('#confirmationDialog').dialog({

and then use:

myDialog.dialog('close');

inside that handler to close the first dialog.

Upvotes: 4

Anto Subash
Anto Subash

Reputation: 3215

In the getJSON callback close the window

$.getJSON( "test/demo", function( data) {
     if(data==='success'){
         $( ".selector" ).dialog( "close" );
         $( ".selector" ).dialog( "open" );
     }
});

To close the jquery UI dialog use this

$( ".selector" ).dialog( "close" );

Top Open a new dialog

$( ".selector" ).dialog( "open" );

for more info check the api of jquery UI http://api.jqueryui.com/dialog/#method-close

Upvotes: 0

Related Questions