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 as I can undestand from MSDN, this is what this option do.
In my application, OnSuccess script fires too early, immediately after the request is sent. I put to sleep the thread for a while, to better see the result. I supposed to see the OnSuccess script (an alert) fired after that sleep time, say, 2 seconds. I use Firebug to see what's happening under the hood, and all I see is: I click the button (submit inside the ajax form), alert is shown (2 seconds before expected), after 2 seconds firebug shows the request-response and nothing more happens.
Where I'm wrong?
Thanks in advance, mt
Some sample code, as correctly asked (I started a blank new solution using Asp.NET MVC template in VS):
Home/Index view:
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<div id="divPlaceholder">
<%
using (Ajax.BeginForm("DoLongTask", "Home",
new AjaxOptions() {
UpdateTargetId = "divPlaceholder",
InsertionMode = InsertionMode.Replace,
OnSuccess = "alert('onsuccess fired')"
}))
{
%>
<input type="submit" value="button" />
<% } %>
</div>
</asp:Content>
HomeController
public ActionResult DoLongTask()
{
if (Request.IsAjaxRequest())
{
System.Threading.Thread.Sleep(2000);
return View();
}
else
{
throw new NotSupportedException();
}
}
I think this is enough to reproduce the behavior. An empty partial view can be used as DoLongTask view.
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'); }"
}))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With