how to use Ajax.BeginForm in MVC3 Razor

In Ajax.BeginForm, you can specify the following arguments

1. The name of the action method that will handle the request.
2. The name of the controller.
3. ajaxOptions : An object that provides options for the asynchronous request.

There are many overloads to this , which can be seen here….

Syntax of the BeginForm is

public static MvcForm BeginForm(
this AjaxHelper ajaxHelper,
string actionName,
string controllerName,
AjaxOptions ajaxOptions
using (Ajax.BeginForm("ActionName", "ControllerName", new AjaxOptions {HttpMethod = "POST", UpdateTargetId = "myDivId" }))
// all other input elements + submit buttons can go in here


In AjaxOption there are various methods like
1. OnBegin
2. OnComplete
3. OnSuccess
4. OnFailure

1. Lets take some examples for using this methods

Lets say we want to validate something when submit button is clicked or it is posted back.
For this we can use OnBegin method. Using this method we can call Javascript Validation & depending on output of the javascript method we can decide whether to call action or not.

Following is example code of the OnBegin Method.

using (Ajax.BeginForm("ActionName", "ControllerName", new AjaxOptions { OnBegin = "return Validate()", HttpMethod = "POST", UpdateTargetId = "myDivId" }))
// all other input elements + submit buttons can go in here

function Validate() {

if(some validation){
return false;

else {
return true;

Here when user click submit button then then before calling the action Onbegin method is called, If it returns true then only action will be called else it will be not called

2. If we want to show some message after action is successful or failure then we can use OnSuccess & OnFailure depending on the conditions.

3. If we want that some thing should happen after the action is complete irrespective of whether it is success or failure the we should use OnComplete

Detailed explanation can be found on