ASP.NET - Ajax.BeginForm的onSuccess回调与PARAMS回调、Ajax、NET、ASP

2023-09-04 00:40:20 作者:不期待.再重来

我想添加更多的PARAMS我的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的功能:

The JS function:

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

ajaxContext 为null。 我该怎么办呢?

But ajaxContext is null. How do I do that?

推荐答案

由于您使用 get_response()我猜你不使用不显眼JavaScript的东西(在MVC3你设置 HtmlHelper.UnobtrusiveJavaScriptEnabled = FALSE ),你就引用MicrosoftAjax,js和MicrosoftMvcAjax.js文件。如果是这样的话,你只需要删除关键字。

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不显眼的JavaScript的支持,那么你可以使用隐式可用 XHR 数据变量来代替。

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