如何使用Ajax.BeginForm OnSuccess 和OnFailure 方法?
我使用这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OnSuccess 和 OnFailure 看起来像是需要 javascript 回调函数。
Pro ASP.NET Framework 第 425 页的示例
ASP.NET AjaxOptions 类
添加控制器示例
最简单的方法是这是我在这里得到的,但我建议使用某种 ViewModel 来研究强类型的 mvc 视图,也许还可以研究一下将 jQuery 用于 ajax。话虽如此,这希望对您有用。
在你看来
The OnSuccess and OnFailure looks like they are expecting javascript callback functions.
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.
In your view
我也在寻找相同的答案,但看起来 Ajax.BeginForm() .. 的事件没有很好的记录,或者需要更多的自我实验来找出这些 onSuccess 和 onFailure 事件何时被调用。但我有一个非常简单直接的替代方案,无需费心设置 AjaxOptions 的 onSuccess 和 onFailure 属性。相反,在控制器的操作方法中,只需通过将 ActionResult 作为 JavaScriptResult 发送来调用 onSuccess()、onFailure() javascript 方法。例如,
Ajax.BeginForm 标记应如下所示
现在,您需要在页面中定义 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,
And the Ajax.BeginForm tag should look like
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.