RELATED: "ASP.NET MVC: Ready for the Enterprise" and "AJAX Features in ASP.NET MVC."
There are only two ways in which a web page can place an HTTP request to a server-side URL. You can set the target URL as the action attribute of an HTML form or you can instruct the browser s XMLHttpRequest object to reach the URL using an HTTP request. The latter scenario represents the typical AJAX scenario, where the client page gains control over the entire operation. In that case, a piece of JavaScript code does the trick of invoking the URL, passes some input data, and gets any response. Implementing this scenario in ASP.NET MVC only requires you to get familiar with and use some ad hoc tools, like the AJAX methods in the jQuery library, or in any other advanced JavaScript library with which you feel comfortable.
What if, instead, you re a server-side person and don t like JavaScript that much? ASP.NET MVC provides an alternative API to add AJAX capabilities to pages, while still remaining in the realm of markup and server code.
In this article, I ll examine an AJAX-related feature of the ASP.NET MVC Framework that basically implements a form of partial rendering on top of the new ASP.NET MVC programming model. In a nutshell, I ll discuss how to post the content of an HTML form to a server controller and update the current view without incurring a full page refresh.
The HTML Message Pattern
Before I go any further, let me briefly recall the underlying pattern we are silently applying here. As thoroughly described at www.ajaxpatterns.org, the HTML Message pattern refers to a situation in which a web page invokes a remote URL and receives a plain HTML response. The URL, whether be it a web/WCF service or a plain REST service, doesn t return data only, but rather a UI-ready string that the caller will take and render out.
HTML Message is the pattern living behind the partial rendering approach in classic ASP.NET, and also is the pattern that some commercial libraries of controls implement to give you AJAX-enabled server controls. Two libraries that do so are Telerik RadControls and Gaiaware.
AJAX can be done in either of two ways: bring plain data to the client and arrange the UI or bring pre-arranged HTML on the client. Partial rendering clearly addresses the second option, and so it is when you use AJAX facilities in the ASP.NET MVC Framework.
The AJAX BeginForm Helper
AJAX support for ASP.NET MVC views is built in to the Ajax helper class. The class features the members listed in the table in Figure 1.
Member |
Description |
ActionLink |
Generates an anchor tag whose action URL is based on the specified parameters. |
BeginForm |
Generates a |
RouteLink |
Generates an anchor tag whose action URL is based on the specified route name. |
ViewContext |
An object that contains information about the view data, controller, and temporary data. |
Figure 1: Members of the Ajax helper class
In particular, the BeginForm helper method generates an AJAX-enabled form tag. Here s how to use it:
<% using (Ajax.BeginForm("GetCustomerDetails", new AjaxOptions { Confirm = "Are you sure?", LoadingElementId = "lblWait", UpdateTargetId = "pnlDetails" })) { %> <% } %>
The first argument to BeginForm indicates the action you want to execute once the form is posted. The name of the target controller can be explicitly mentioned using one of the numerous overloads of BeginForm. If not specified, it is inferred from the content of ViewContext.
<% = Ajax.ViewContext.Controller.ToString() %>
If not explicitly specified, the default controller is the controller that ordered the rendering of the current view. A second fundamental parameter for the BeginForm method is an instance of the AjaxOptions class. The members of the AjaxOptions class are detailed in the table in Figure 2. Essentially, the AjaxOptions class lets you specify information that will help the framework carry the operation the way you want. At the very minimum, you might want to indicate the ID of the element that will receive any HTML message that the controller method may have generated on the server. The ID is set through the UpdateTargetId member of the AjaxOptions class.
HttpMethod |
String property, indicates the HTTP operation to be performed. The property value is set to POST by default. |
InsertionMode |
Indicates how any HTML response should be inserted in the current page DOM. Feasible values for the property come from the InsertionMode enumerated type: Replace, InsertBefore, InsertAfter. The element to replace is the element pointed by the UpdateTargetId property. The default value is Replace. |
LoadingElementId |
String property, gets and sets the ID of the DOM element to be displayed for the time it takes to complete the request. |
OnBegin |
String property, gets and sets the name of an optional JavaScript function to be executed just before submitting the request. |
OnComplete |
String property, gets and sets the name of an optional JavaScript function to be executed once the request has completed. |
OnFailure |
String property, gets and sets the name of an optional JavaScript function to be executed in case of a failed request. |
OnSuccess |
String property, gets and sets the name of an optional JavaScript function to be executed if the request completes successfully. |
UpdateTargetId |
String property, gets and sets the ID of the DOM element to be updated with any HTML response coming back from the server. |
Url |
String property, gets and sets the actual URL the request should be sent to. If specified, the property takes priority over the action attribute of the |
Figure 2: Members of the AjaxOptions class
I also declared the LoadingElementId property in the preceding code snippet. This property indicates the ID of the user interface element you want to display temporarily as the request goes. The role of the LoadingElementId property has some analogy with the UpdateProgress control you may recall from classic ASP.NET partial rendering. Given the preceding code snippet that uses Ajax.BeginForm, the corresponding resulting markup you ll find in the browser is shown in Figure 3.
Figure 3: The resulting markup when using Ajax.BeginForm
As you can see, the onsubmit attribute points to a framework-provided object the Sys.Mvc.AsyncForm object. The object is defined in the MicrosoftMvcAjax.js file that is referenced automatically from any AJAX-enabled ASP.NET MVC page. Figure 4 shows the source code of the class.
Sys.Mvc.AsyncForm = function Sys_Mvc_AsyncForm() { } Sys.Mvc.AsyncForm.handleSubmit = function Sys_Mvc_AsyncForm$handleSubmit(form, evt, ajaxOptions) { /// /// /// /// /// /// evt.preventDefault(); var body = Sys.Mvc.MvcHelpers._serializeForm(form); Sys.Mvc.MvcHelpers._asyncRequest(form.action, form.method || 'post', body, form, ajaxOptions); }
Figure 4: The AsyncForm class
Without going into the nitty-gritty details of the internal members, the overall behavior of the object comes out quite clearly. The handleSubmit method first prevents the default browser event from taking place (so no browser post will ever occur). Next, it serializes to a string the current content of the