[英]ASP.NET - Ajax.BeginForm OnSuccess call back with params
I want to add more params to my OnSuccess call back ( but keep the ajax context variable ). 我想在OnSuccess回调中添加更多参数( 但保留ajax上下文变量 )。
What I did is: 我所做的是:
using (Ajax.BeginForm("Register", new AjaxOptions() {
OnSuccess = "new function(arg){HandleBasicForm(arg , 'MyCustomVariable')}",
...
The JS function: JS函数:
function HandleBasicForm(ajaxContext , myCustomVariable){
var content = ajaxContext.get_response().get_object();
....
}
But ajaxContext
is null. 但是
ajaxContext
为null。
How do I do that? 我怎么做?
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. 由于您使用的是
get_response()
所以我猜您不是在使用非干扰性的javascript东西(在MVC3中,您已将HtmlHelper.UnobtrusiveJavaScriptEnabled = false
设置HtmlHelper.UnobtrusiveJavaScriptEnabled = false
),并且您正在引用MicrosoftAjax,js和MicrosoftMvcAjax.js文件。 If that's the case you just need to drop the new
keyword. 如果是这种情况,您只需删除
new
关键字。
using (Ajax.BeginForm("Register", new AjaxOptions() { OnSuccess = "function(arg){HandleBasicForm(arg , 'MyCustomVariable')}"})
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. 如果您通过jquery.unobtrusive-ajax.js使用MVC3非侵入式javascript支持,则可以改用隐式可用的
xhr
和data
变量。
using (Ajax.BeginForm("Register", new AjaxOptions() { OnSuccess = "HandleBasicForm(data, 'MyCustomVariable')"})
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. 在您的处理程序中,无需使用
get_response().get_object()
因为反序列化的JSON数据将直接传递到您的处理程序中。
function HandleBasicForm(data, myCustomVariable){
var someValue = data.someProperty; //work with data object returned
....
}
OnSuccess
receives data, status, xhr
from the server: OnSuccess
从服务器接收data, status, xhr
:
OnSuccess = "myJsMethod(data, status, xhr)"
And then its equivalent JavaScript method would be: 然后,其等效的JavaScript方法将是:
function myJsMethod(data, status, xhr) {
}
Now your controller should return: 现在,您的控制器应该返回:
return Json(new { param1 = 1, param2 = 2, ... }, JsonRequestBehavior.AllowGet);
Then in myJsMethod
you will have access to data.param1
and so on. 然后,在
myJsMethod
您可以访问data.param1
,依此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.