skip to the main content area of this page
Patterns and Practices

 

ASP.NET MVC Framework with ASP.NET AJAX Support - AjaxController


Nikhil Kothari shows off the ASP.NET AJAX Support in the ASP.NET MVC Framework with concepts like AjaxController and properties and extension methods like IsAjaxRequest, RenderPartial, RenderBeginAjaxForm, various Behaviors, etc.

For those interested in taking advantage of ASP.NET AJAX, controllers will be derived from AjaxController instead of the Controller Base Class. This provides access to a IsAjaxRequest Property which helps one determine if the request is an asynchronous postback request:

 

public class CustomerController : AjaxController {

    //...

    [ControllerAction]
    public void List() {
        if (IsAjaxRequest) {
            RenderPartial("List", _customerService.GetCustomers());
        else 
            RenderView("CustomersView", _customerService.GetCustomers());
    }
}

 

To issue XMLHttp requests rather than a regular form submit there are a few extension methods: RenderBeginForm which renders a regular form tag, RenderBeginAjaxForm which renders an Ajax-enabled form, and RenderEndForm.

 

<% RenderBeginAjaxForm(Url.Action("Add"),
    new { Update="customerList, UpdateType="appendBottom",
        Highlight="True",
        Starting="startAddCustomer",
        Completed="endAddCustomer" }); %>
   <input type="text" name="name" />
   <input type="submit" name="addCustomer" value="Add Customer" />
<% RenderEndForm(); %>

 

For more information in the AjaxController and the ASP.NET AJAX support in the ASP.NET MVC Framework, check out Nikhil's post.


Tags: AJAX, MVC


Topics



 

Popular Tags



Recent Links