I\'d like to use the OnSuccess option of AjaxOptions passed as Ajax.BeginForm argument to \"do something\" once the response is completely received and DOM updated. As far a
Call a function Like
function showAlert(){ alert("Hello World"); }Call in Ajax Begin
onSuccess="showAlert"
without parentheses
OnSuccess is a property which takes the name of the function or the function as in the above case (not any statment). And AjaxOptions callsback the method for which name is provided, after success, failure, ... Hope this helps
I have been looking in to this problem and it seems that the "On" events are not quite what they seem.
If you read this post How is OnSuccess measured for a ASP.NET MVC ActionLink? you will see womp saying that the events may fire no matter what happens in the controller. I have also found this in the tests i did by adding all the "On" events to the AjaxOptions Object like this:
new AjaxOptions()
{
UpdateTargetId = "divPlaceholder",
InsertionMode = InsertionMode.Replace,
OnSuccess = "alert('OnSuccess')",
OnBegin = "alert('OnBegin')",
OnComplete = "alert('OnComplete')",
OnFailure = "alert('OnFailure')"
}
I have not used the AjaxOptions in any production code I've written but I have had great success with jQuery Ajax calls and the events firing at the correct times.
A tutorial on ASP.net MVC and jQuery Ajax can be found here and some examples of how to use events can be found in code on this question
Sorry I've not been able to fix your problem but I hope this alternative will help.
Not sure how helpful this will be but I am looking at doing something like this at the moment and have found if you call out to a function within the OnSucess then it will happen after the action method, for example:
using (Ajax.BeginForm("DoLongTask", "Home",
new AjaxOptions() {
UpdateTargetId = "divPlaceholder",
InsertionMode = InsertionMode.Replace,
OnSuccess = "function() { alert('onsuccess fired'); }"
}))
Just tested the OnSuccess callback in MVC 3 and it seems to be fixed.