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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5517813/
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-08-26 19:17:07  来源:igfitidea点击:

How to Use Ajax.BeginForm OnSuccess and OnFailure Methods?

asp.netajaxasp.net-mvc-2jquery

提问by kumar

I using this Ajax.BeginForm

我使用这个 Ajax.BeginForm

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

<% } %>

What do I need to write in the controler to catch this OnSucces and OnFailure.

我需要在控制器中写什么来捕捉这个 OnSucces 和 OnFailure。

Because OnSuccess I need to show Success message

因为 OnSuccess 我需要显示成功消息

OnFailure I need to show othere message.

OnFailure 我需要显示其他消息。

In my Controller

在我的控制器中

Public ActionResult GetSomething(FromCollection collection)
{
     if(exists == null)
     {
          //OnSuccess
     }
     else
     { 
         //OnFailure
     }
}

Can anydboy help me out.. how to catch this?

anydboy 可以帮我解决这个问题吗?

Thanks

谢谢

回答by Nicky Waites

The OnSuccess and OnFailure looks like they are expecting javascript callback functions.

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"}) %>

Example from Pro ASP.NET Frameworkpage 425

来自Pro ASP.NET Framework第 425 页的示例

ASP.NET AjaxOptions Class

ASP.NET AjaxOptions 类



Added Controller Example

添加控制器示例

The simpliest way to do this would be what I've got here but I recommend looking into strongly typed mvc views using some kind of ViewModel and maybe look into using jQuery for your ajax. With that said this should hopefully work for you.

最简单的方法就是我在这里得到的,但我建议使用某种 ViewModel 来查看强类型的 mvc 视图,并可能考虑将 jQuery 用于您的 ajax。话虽如此,这应该希望对你有用。

if (exists)
{
  ViewData["msg"] = "Some Success Message";
}
else
{
  ViewData["msg"] = "Some Error Message";
}

return View();

In your view

在你看来

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

回答by Emran Hussain

I was also searching for the same answer, but looks like Ajax.BeginForm() .. 's event's are not well documented or need more self experiments to find out when these onSuccess and onFailure events are called. But I got a very easy and straight forward alternative for not to bother with setting onSuccess and onFailure properties of the AjaxOptions. Rather, in your Controller's action method, simply call the onSuccess(), onFailure() javascript method by sending ActionResult as JavaScriptResult. For example,

我也在寻找相同的答案,但看起来像 Ajax.BeginForm() .. 的事件没有很好的记录或需要更多的自我实验来找出何时调用这些 onSuccess 和 onFailure 事件。但是我得到了一个非常简单直接的替代方法,不用费心设置 AjaxOptions 的 onSuccess 和 onFailure 属性。相反,在您的控制器的操作方法中,只需通过将 ActionResult 作为 JavaScriptResult 发送来调用 onSuccess()、onFailure() javascript 方法。例如,

Public ActionResult Create(FromCollection collection)
{
     if(exists == null)
     {
          //OnSuccess
           return JavaScript("OnSuccess();");
     }
     else
     { 
         //OnFailure
         return JavaScript("OnFailure();");
     }
}

And the Ajax.BeginForm tag should look like

Ajax.BeginForm 标签应该看起来像

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

{%>

<% } %>

Now, you need to define the OnSuccess() and OnFailure() javascript methods in your page and thats it.

现在,您需要在页面中定义 OnSuccess() 和 OnFailure() javascript 方法,仅此而已。

EDIT:

编辑:

I was thinking, perhaps, OnSuccess() will be called by default if no Exception is thrown from the Server. OnFailure() will be called if any Exception is thrown from the server. I did not test this concept yet. If that is true, then, it wont be a good idea to practice sending JavaScript("OnSuccess();") and JavaScript("OnFailure();"); from the server, because that will not be a good pattern to follow.

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