Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何使用Ajax.BeginForm OnSuccess和OnFailure方法?_Asp.net_Ajax_Asp.net Mvc 2_Jquery - Fatal编程技术网

Asp.net 如何使用Ajax.BeginForm OnSuccess和OnFailure方法?

Asp.net 如何使用Ajax.BeginForm OnSuccess和OnFailure方法?,asp.net,ajax,asp.net-mvc-2,jquery,Asp.net,Ajax,Asp.net Mvc 2,Jquery,我正在使用这个Ajax.begin <% using( Ajax.BeginForm( "Create","Mandate", new AjaxOptions( ) { OnSuccess = "GoToMandates", OnFailure = "ShowPopUpError" }

我正在使用这个Ajax.begin

    <% using( Ajax.BeginForm( "Create","Mandate",
                       new AjaxOptions( ) {
                           OnSuccess = "GoToMandates",
                           OnFailure = "ShowPopUpError"
                       } ) ) {%>

<% } %>
有人能帮我吗。。怎么抓住这个


谢谢

OnSuccess和OnFailure看起来像是在期待javascript回调函数

<script type="text/javascript">
    function handleError(ajaxContext) {
    var response = ajaxContext.get_response();
    var statusCode = response.get_statusCode();
    alert("Sorry, the request failed with status code " + statusCode);
    }
</script>

<%= Ajax.ActionLink("Click me", "MyAction",
new AjaxOptions { UpdateTargetId = "myElement", OnFailure = "handleError"}) %>
在你看来

<div id="myResults" style="border: 2px dotted red; padding: .5em;">
    <%: ViewData["msg"]%>
</div>

我也在搜索相同的答案,但看起来像Ajax.BeginForm()的事件没有很好的文档记录,或者需要更多的自我实验来确定何时调用这些onSuccess和onFailure事件。但我得到了一个非常简单和直接的替代方案,不用麻烦设置AjaxOptions的onSuccess和onFailure属性。相反,在控制器的action方法中,只需通过将ActionResult作为JavaScriptResult发送来调用onSuccess(),onFailure()javascript方法。比如说,

Public ActionResult Create(FromCollection collection)
{
     if(exists == null)
     {
          //OnSuccess
           return JavaScript("OnSuccess();");
     }
     else
     { 
         //OnFailure
         return JavaScript("OnFailure();");
     }
}
而Ajax.BeginForm标记应该如下所示

 <%
  using( Ajax.BeginForm( "Create","Mandate", new AjaxOptions())) // see, no OnSuccess and OnFailure here.

{%>

<% } %>

现在,您需要在页面中定义OnSuccess()和OnFailure()javascript方法

编辑:


我在想,如果服务器没有抛出异常,默认情况下会调用OnSuccess()。如果从服务器引发任何异常,将调用OnFailure()。我还没有测试这个概念。如果这是真的,那么,练习发送JavaScript(“OnSuccess();”)和JavaScript(“OnFailure();”)就不是一个好主意;从服务器,因为这不是一个好的模式。

谢谢NIcky,我需要在我的控制器中写些什么?谢谢,好的,我知道你想在那里做什么。我相信AjaxOptionSuccess和OnError只与XHR请求相关,与控制器无关。我会快速思考,然后更新我的答案。可以调用jquery函数吗?@jvelez具体做什么?ajax请求?如果您想要类似的东西,请查看jqueryformplugin,否则jqueryajax也可以工作。
Public ActionResult Create(FromCollection collection)
{
     if(exists == null)
     {
          //OnSuccess
           return JavaScript("OnSuccess();");
     }
     else
     { 
         //OnFailure
         return JavaScript("OnFailure();");
     }
}
 <%
  using( Ajax.BeginForm( "Create","Mandate", new AjaxOptions())) // see, no OnSuccess and OnFailure here.

{%>

<% } %>