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

发布于 2024-10-29 02:13:13 字数 676 浏览 1 评论 0原文

我使用这个 Ajax.BeginForm

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

<% } %>

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

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

OnFailure 我需要显示其他消息。

在我的控制器中,

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

anydboy 可以帮我吗..如何捕获这个?

谢谢

I using this 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.

Because OnSuccess I need to show Success message

OnFailure I need to show othere message.

In my Controller

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

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夢归不見 2024-11-05 02:13:13

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

Pro ASP.NET Framework 第 425 页的示例

ASP.NET AjaxOptions 类


添加控制器示例

最简单的方法是这是我在这里得到的,但我建议使用某种 ViewModel 来研究强类型的 mvc 视图,也许还可以研究一下将 jQuery 用于 ajax。话虽如此,这希望对您有用。

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

return View();

在你看来

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

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

<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 Framework page 425

ASP.NET AjaxOptions Class


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.

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>
绅刃 2024-11-05 02:13:13

我也在寻找相同的答案,但看起来 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();");
     }
}

Ajax.BeginForm 标记应如下所示

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

{%>

<% } %>

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

编辑:

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

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,

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

 <%
  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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文