javascript - How to close a jQuery dialog after an AJAX JSON call -
i using asp.net mvc 4
, jquery
, , jquery ui
.
i have dialog on view. when click button dialog pops up, takes values on dialog , send through service. service needs , either send blank message if successful or actual error message. after need check error on client side, close current dialog , open success dialog or error dialog. i'm not sure how close current dialog , display 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) { // error message resource file message = ... } return json(message, jsonrequestbehavior.allowget); }
how close current pop after json call , open another?
you have add dialog page first: put prior current:
$('#errordialog').dialog({ autoopen: false, modal: true, resizable: false, width: 330, title: 'my error dialog' }); //current code follows: $('#confirmationdialog').dialog({
then have should work.
edit: thought bit, need fix scope of $(this)
inside success handler.
change do:
var mydialog = $('#confirmationdialog').dialog({
and use:
mydialog.dialog('close');
inside handler close first dialog.
Comments
Post a Comment