Asp.NET MVC AjaxOptions的onSuccess火灾..太早了? [英] Asp.NET MVC AjaxOptions OnSuccess fires.. too early?

查看:164
本文介绍了Asp.NET MVC AjaxOptions的onSuccess火灾..太早了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的Ajax.BeginForm参数做点什么一旦反应已完全接收并更新DOM通过AjaxOptions的的onSuccess选项。至于我可以从MSDN已了解,这是该选择做什么。

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.

在我的应用程序,脚本的onSuccess火灾太早,请求被发送后即可。我把睡觉线程一会儿,以便更好地查看结果。我应该看到,睡眠时间后,解雇了的onSuccess脚本(警报),说,2秒。我使用Firebug,看看有什么引擎盖下发生的,而我看到的是:我单击按钮(提交ajax的表单中),显示警报(2秒之前的预期),2秒后萤火虫显示请求 - 响应并没有什么更多的情况。

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.

在哪里我错了?

在此先感谢,
MT

Thanks in advance, mt

一些示例code,如正确地问(我使用VS Asp.NET MVC模板开始一个空白的新的解决方案):

Some sample code, as correctly asked (I started a blank new solution using Asp.NET MVC template in VS):

首页/索引视图:

<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();
    }
}

我认为这是足以重现行为。空部分视图可用作DoLongTask图。

I think this is enough to reproduce the behavior. An empty partial view can be used as DoLongTask view.

推荐答案

我一直在寻找这个问题,似乎开事件是不是很象它看上去那样。

I have been looking in to this problem and it seems that the "On" events are not quite what they seem.

如果你看到这篇文章<一个href=\"http://stackoverflow.com/questions/1183973/how-is-onsuccess-measured-for-a-asp-net-mvc-actionlink\">http://stackoverflow.com/questions/1183973/how-is-onsuccess-measured-for-a-asp-net-mvc-actionlink你会看到womp说​​,事件可能触发不管​​在控制器中发生的。我也发现了这个在测试中我没有加入所有的开事件到AjaxOptions对象是这样的:

If you read this post http://stackoverflow.com/questions/1183973/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')"

 }

我没用过,我写在任何生产code中的AjaxOptions但我有使用jQuery Ajax调用,并在正确的时间触发的事件了巨大的成功。

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.

这是ASP.net MVC和jQuery阿贾克斯教程可以发现<一个href=\"http://weblogs.asp.net/mikebosch/archive/2008/02/15/asp-net-mvc-submitting-ajax-form-with-jquery.aspx\">here以及如何使用事件的一些例子可以在code被发现的this问题

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.

这篇关于Asp.NET MVC AjaxOptions的onSuccess火灾..太早了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆