Fork me on GitHub

Ajax.BeginForm AjaxOptions custom arguments for OnComplete, OnSuccess, OnFailure and OnBegin 6

by

Some interesting behavior I noticed recently about the Microsoft MVC 3 jQuery library jquery.unobtrusive-ajax.js is the ability to pass you own arguments into the handler.

Normally you have probably used it like this:

This works pretty well but it’s not very clear if/how you could pass additional arguments into the OnComplete (possibly to get some code reuse anyone?). If you take a look at the jquery.unobtrusive-ajax.js source however it becomes apparent that you can actually pass any (*see notes at end of post) arguments you want into your OnComplete.

 

Here is an example of just that:

Here is what the OnComplete JavaScript function looks like:

 

This should work fine for all the types of events specified in AjaxOptions, but different argument names are available for the different events:

  • OnBegin – xhr
  • OnComplete – xhr, status
  • OnSuccess – data, status, xhr
  • OnFailure – xhr, status, error

 

*I have noticed one problem with passing arguments, do not include a ‘.’ in any passed arguments. The function ‘getFunction’ does a split on ‘.’ on passed arguments presumably for some JavaScript namespacing/method purposes?

6 thoughts on “Ajax.BeginForm AjaxOptions custom arguments for OnComplete, OnSuccess, OnFailure and OnBegin

  1. Reply Lakshmi NarsimhaRaju Dantuluri May 20,2016 1:11 am

    If I have 2 Buttons
    I binded to both button AAK and Button PAAK
    Now when I click karivepak what will happen
    DLN Raju

  2. Reply RamiReddy Dec 19,2014 9:32 am

    Hi,

    If we have multiple buttons how to idenfity which button is clicked in OnComplete mehtod.

    Thanks,
    RamiReddy

  3. Reply Michael Oct 21,2014 7:15 pm

    Scoured the net and couldn’t find the OnFailure parameters in order to get the actual error message, until I found this. Thanks!!

  4. Reply SF Mar 26,2014 8:54 am

    Great stuff, this helped me a lot.

  5. Reply peter Oct 15,2013 10:20 am

    Thanks mate. Very helpful.

Leave a Reply