asp.net-mvc Asp.NET MVC AjaxOptions OnSuccess 触发.. 为时过早?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1200960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:44:50  来源:igfitidea点击:

Asp.NET MVC AjaxOptions OnSuccess fires.. too early?

asp.net-mvcasp.net-ajax

提问by ccalboni

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.

我想使用作为 Ajax.BeginForm 参数传递的 AjaxOptions 的 OnSuccess 选项来“做某事”,一旦完全收到响应并更新 DOM。据我从 MSDN 了解,这就是这个选项的作用。

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.

在我的应用程序中, OnSuccess 脚本在发送请求后立即触发太早。我让线程休眠一段时间,以更好地查看结果。我应该看到 OnSuccess 脚本(警报)在该睡眠时间(例如 2 秒)之后触发。我使用 Firebug 来查看幕后发生的事情,我看到的只是:我单击按钮(在 ajax 表单内提交),显示警报(比预期提前 2 秒),2 秒后 firebug 显示请求-响应,什么也没有更多发生。

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):

一些示例代码,如正确要求(我在 VS 中使用 Asp.NET MVC 模板开始了一个空白的新解决方案):

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.

我认为这足以重现该行为。空的局部视图可以用作 DoLongTask 视图。

采纳答案by Webmonger

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:

如果您阅读了这篇文章如何衡量 ASP.NET MVC ActionLink 的 OnSuccess?你会看到 womp 说无论控制器中发生什么,事件都可能会触发。我还在测试中发现了这一点,我通过将所有“On”事件添加到 AjaxOptions 对象中,如下所示:

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.

我没有在我编写的任何生产代码中使用 AjaxOptions,但是我在 jQuery Ajax 调用和在正确时间触发的事件方面取得了巨大的成功。

A tutorial on ASP.net MVC and jQuery Ajax can be found hereand some examples of how to use events can be found in code on this question

可以在此处找到有关 ASP.net MVC 和 jQuery Ajax 的教程,在此问题的代码中可以找到有关如何使用事件的一些示例

Sorry I've not been able to fix your problem but I hope this alternative will help.

抱歉,我无法解决您的问题,但我希望此替代方法会有所帮助。

回答by Paul McCaskie

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:

不确定这会有多大帮助,但我目前正在考虑做这样的事情,并且发现如果您调用 OnSucess 中的函数,那么它将在 action 方法之后发生,例如:

using (Ajax.BeginForm("DoLongTask", "Home", 
    new AjaxOptions() {
        UpdateTargetId = "divPlaceholder", 
        InsertionMode = InsertionMode.Replace,
        OnSuccess = "function() { alert('onsuccess fired'); }"
    })) 

回答by Muhammed Ali

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

OnSuccess 是一个属性,它采用函数或函数的名称,如上述情况(不是任何语句)。并且 AjaxOptions 回调为其提供名称的方法,成功,失败后,...希望这会有所帮助

回答by Samme

Call a function Like

调用一个函数喜欢

function showAlert(){ alert("Hello World"); }

Call in Ajax Begin

调用 Ajax 开始

onSuccess="showAlert"

onSuccess="showAlert"

without parentheses

没有括号

回答by nthpixel

Just tested the OnSuccess callback in MVC 3 and it seems to be fixed.

刚刚在 MVC 3 中测试了 OnSuccess 回调,它似乎已修复。