This is going to be a fairly long post explaining the various features of jqModal and how it integrates with ASP.NET MVC. jqModal is a fairly robust, lightweight library that allows easy integration of modal popups with your web-apps. You can download jqModal from HERE
In this example, we demonstrate how jqModal can be used on an MVC page to show a form that allows the user to add a line item to a given order. The scenario is as follows:
1. The user clicks on a hyperlink “Add Line Item”.
2. jqModal opens up and loads the partial view containing the add line item form at runtime.
3. User enters items in the form and then hits “Save”. The modal form uses ajax and inserts the line item into the DB and displays a message to the user.
4. User clicks on Close to close the modal popup.
5. The main page uses ajax and reloads the line items.
This is a fairly common scenario in most web applications.
Without further ado, let’s dive into the code.
Let’s place a simple “Add a Line Item” link on the page. This link is solely responsible for opening the jqModal. As such this link should be decorated with the ‘jqModal’ css class.
<a href="#" class="jqModal">Add a Line Item</a>
The next step would be do add the container DIV that will be used to show the modal dialog. This can be easily set using the code as shown:
<div id="addLineItemControlSection" class="jqmWindow">
</div>
It is important to note that a css-class of ‘jqmWindow’ needs to be associated with the div that will be displayed in the modal popup.
Now, the jqModal site clearly states that we need to initialize the jqModal setup ONLY ONCE when the page loads. Any setups there-after will be considered as updates. To wire up the page to trigger the dialog box when the link “Add a Line Item” is clicked, we initialize jqmodal using the following:
$().ready(function() {
$('#addLineItemControlSection').jqm({ modal: true,
ajax: '<%= Url.Action(ActionNames.Add,
ControllerNames.Detail, new { invoiceId = Model.Invoice.InvoiceId }) %>',
onHide: myAddClose
});
});
The above code does a couple of interesting things. On the ‘ready’ event, it wires up the jqModal with the ‘#addLineItemControlSection’ (which is the DIV that will load as modal popup).
The modal: true property indicates that the window will be opened as a modal window and will not be closed until “Close” is explicitly clicked.
The ajax property also allows me to pass the Action method that will be used to display the modal popup. We use an AJAX call to build the modal popup.
The onHide property allows me to fire a javascript after the modal popup is closed. Typically you can use this in scenarios where you want to update the parent window after the child modal popup is closed. In our case, I use this onHide to update the container div containing the line items so that it displays the newly added line item.
As you can see in the controller code below, my modal popup just displays a “partial view” that I return from the controller when I call the Action “Add”.
public ActionResult Add(int invoiceId)
{
AddInvoiceLineItemViewModel model = new AddInvoiceLineItemViewModel();
model.LineItems = invoiceLogicInstance.GetLineItemsByInvoiceId(invoiceId);
model.InvoiceId = invoiceId;
model.CurrentInvoiceItem = new InvoiceItem();
return View(ViewNames.InvoiceLineItemControl, model);
}
I return a View called InvoiceLineItemControl which is a partial view and is declared as follows:

As you can see, I am using Ajax.BeginForm to save my line item to the database. On a successful insert, I actually replace the entire lineItemSaveDiv and replace it with new HTML.
Notice that the close link (as decorated by “jqmClose”) line is kept outside the lineItemSaveDiv. This is because after the save is done, I don’t want the link to disappear once I update the result on the modal popup. This will allow me to close the modal window.
In any case, your modal dialog should have the following code, allowing the window to be closed.
<a href="#" class="jqmClose">Close</a>
Once the window is closed, the function set in the onHide property will be called. Here’s the code to update the parent window:
function myAddClose(hash) {
$('#lineItemLoadingDiv').show();
var test = Math.floor(Math.random()*101);
$.get('/Invoicing/Detail/ShowLineItems?invoiceId=' + <%= Model.Invoice.InvoiceId
%> + '&item=' + test, null,
function(data) {
$('#invoiceItemsDiv').html(data);
});
hash.w.fadeOut('1000', function() { hash.o.remove(); });
}
A couple of important things to note here:
1. I am creating a random number and passing that as an action argument to my controller. Why? This is to make the get request unique each time so that IE doesn’t cache the request and bypass the GET calls. To read more about this, please read my post on IE and the JSON Caching Issue
2. I am calling hash.w.fadeOut. What is that? hash.w is nothing but a way to access the jqmodal window. All I am doing is fading the window out and removing the overlay (by accessing hash.o).
More explanation as obtained from jqmodal site is shown here:
Callbacks allow advanced customization of jqModal dialogs. Each callback is passed the “hash” object consisting of the following properties;
* w: (jQuery object) The dialog element
* c: (object) The config object (dialog’s parameters)
* o: (jQuery object) The overlay
* t: (DOM object) The triggering element
Here are some snapshots:
The modal popup containing the ajax generated form:

After Save is hit, the following confirmation is displayed using ajax:

Hopefully that helped. Feel free to put any comments/suggestions!