ASP.NET - Ajax.BeginForm OnSuccess 回调参数 [英] ASP.NET - Ajax.BeginForm OnSuccess call back with params

查看:20
本文介绍了ASP.NET - Ajax.BeginForm OnSuccess 回调参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 OnSuccess 回调中添加更多参数(但要保留 ajax 上下文变量).
我所做的是:

I want to add more params to my OnSuccess call back (but keep the ajax context variable).
What I did is:

 using (Ajax.BeginForm("Register", new AjaxOptions() {
   OnSuccess = "new function(arg){HandleBasicForm(arg , 'MyCustomVariable')}",
    ...

JS 函数:

function HandleBasicForm(ajaxContext , myCustomVariable){
            var content = ajaxContext.get_response().get_object();
            ....
        }

但是 ajaxContext 为空.
我该怎么做?

But ajaxContext is null.
How do I do that?

推荐答案

自从你使用 get_response() 我猜你没有使用不显眼的 javascript 东西(在 MVC3 你'已设置 HtmlHelper.UnobtrusiveJavaScriptEnabled = false) 并且您正在引用 MicrosoftAjax、js 和 MicrosoftMvcAjax.js 文件.如果是这种情况,您只需删除 new 关键字.

Since you're using get_response() I'm guessing that you're not using the unobtrusive javascript stuff (in MVC3 you've set HtmlHelper.UnobtrusiveJavaScriptEnabled = false) and you're referencing the MicrosoftAjax,js and MicrosoftMvcAjax.js files. If that's the case you just need to drop the new keyword.

 using (Ajax.BeginForm("Register", new AjaxOptions() { OnSuccess = "function(arg){HandleBasicForm(arg , 'MyCustomVariable')}"})

如果您在 jquery.unobtrusive-ajax.js 中使用 MVC3 unobtrusive javascript 支持,那么您可以使用隐式可用的 xhrdata 变量.

If you are using the MVC3 unobtrusive javascript support with jquery.unobtrusive-ajax.js then you can use the implicitly available xhr and data variables instead.

using (Ajax.BeginForm("Register", new AjaxOptions() { OnSuccess = "HandleBasicForm(data, 'MyCustomVariable')"})

在您的处理程序中,无需使用 get_response().get_object() 因为反序列化的 JSON 数据将直接传递给您的处理程序.

In your handler there would be no need to use get_response().get_object() since the deserialized JSON data would be passed in to your handler directly.

function HandleBasicForm(data, myCustomVariable){
    var someValue = data.someProperty; //work with data object returned
    ....
}

这篇关于ASP.NET - Ajax.BeginForm OnSuccess 回调参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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