我想添加更多的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
....
}