如何使用Ajax.BeginForm的onSuccess和OnFailure方法? [英] How to Use Ajax.BeginForm OnSuccess and OnFailure Methods?

查看:1333
本文介绍了如何使用Ajax.BeginForm的onSuccess和OnFailure方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个Ajax.BeginForm

I using this Ajax.BeginForm

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

<% } %>

什么我需要在CONTROLER写抓住这个OnSucces和OnFailure。

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

由于的onSuccess我需要证明成功的消息

Because OnSuccess I need to show Success message

OnFailure我需要证明othere消息。

OnFailure I need to show othere message.

在我的控制器

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

可以anydboy帮我..怎么抓呢?

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

感谢

推荐答案

本的onSuccess和OnFailure看起来像他们期待的JavaScript回调函数。

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

临ASP.NET框架页例425

ASP.NET AjaxOptions类

新增控制器为例

要做到这一点是我在这里得到了什么,但我建议寻找到强类型MVC的看法使用某种视图模型,并可能考虑使用jQuery为您阿贾克斯simpliest方式。随着中说你这个应该有希望的工作。

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

在你看来

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

这篇关于如何使用Ajax.BeginForm的onSuccess和OnFailure方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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