I am using jQuery modal dialogs in my app to handle normal CRUD operations. In some cases I have even two stacked modal dialogs open.
I have then created two generic function in an external javascript file to handle respectively the showing and the submit of CRUD forms.
To show modal dialogs I call the following function
function _loadDialog(level, action, id, title, onCloseHandler) {
var panel = panels[level];
$(panel).dialog("option", "title", title);
var url = action;
if (id != "") url = url + "/" + id;
$.ajax({
type: "get",
dataType: "html",
url: url,
data: {},
success: function(response) {
$(panel).html('').html(response).dialog('open');
}
});
$(panel).unbind("dialogclose").bind("dialogclose", function(event, ui) {
if (onCloseHandler != null) {
onCloseHandler();
}
});
}
This function receive, among the others, a level parameter that instruct the function how to stack the dialog and where to place the rendered Partial markup returning back from the ajax call. This function works fine.
Inside the Partial View returned by the ajax call there are input and at the end the following code
<div style="text-align:right;">
<input type="submit" id="btnSave" value="Salva" />
</div>
and, for the jQuery part, for example,
$("#contactForm").submit(function(event) {
_submitForm(1, event, "#contactForm", "post", "html", '<%= Url.Content("~/Contact/Save") %>');
});
As you can see the submit function has the following signature
function _submitForm(level, event, formName, atype, adataType, aurl) {
}
and it handle
- the form submission to the correct controller action
- the user feedback (e.g. "action executed successfully")
- the dialog close operation
The level parameter is needed to address all the function, including closing the dialog, to the correct DIV panel used.
I would like to be able to show the same dialog sometimes as a dialog and sometimes as a sub dialog.
To be able to do this, I mean "logically speaking" as I am not so strong with javascript and jQuery, I need the following changes:
- Modify the
_loadDialogfunction to save thelevelparameter inside the dialog markup itself - Modify the
_submitFormfunction and make it using the correctlevelparameter that has been previously saved from the_loadDialogfunction.
How can I achieve this?
_submitFormcalled from theonCloseHandleryou pass into_loadDialog?_submitFormis called by a click event of a submit button inside the form