[ASP.NET MVC] Unobtrusive Ajax [4] – Ajax Callbacks

By providing event handlers to “AjaxOptions“, you can control each step of Ajax request and response.

1. AjaxOptions Callback Properties

You can add the handler to various points in the Ajax requesting by using “AjaxOptions“.

  • OnBegin: before sending a request
  • OnFailure: when the request fails
  • OnSuccess: after the page is successfully updated
  • OnComplete: when the request has completed (after “OnSuccess” or “OnFailure”)

2. Function Signatures for Handlers

You can create handler functions like these:

<script type="text/javascript">
function OnBegin() {
  alert("Starting...");
}

function OnSuccess(data) {
  alert("Succeeed... " + data);
}

function OnFailure(request, error) {
  alert("Failed... " + error);
}

function OnComplete(request, status) {
  alert("Completed... " + status);
}
</script>

3. Connecting Handlers to AjaxOptions

Once you have JavaScript function, it is very easy to map them to the events.

@{
  ViewBag.Title = "ViewProduct";
  AjaxOptions ajaxOpts = new AjaxOptions {
    UpdateTargetId = "productDetail",
    OnBegin = "OnBegin",
    OnSuccess = "OnSuccess",
    OnFailure = "OnFailure",
    OnComplete = "OnComplete" };
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s