asp.net-mvc 我可以在 Ajax.ActionLink 中通过 OnSuccess 事件传递参数吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/792441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I pass a parameter with the OnSuccess event in a Ajax.ActionLink
提问by Whozumommy
When I use:
当我使用:
new AjaxOptions
{
UpdateTargetId = "VoteCount" + indx,
OnSuccess = "AnimateVoteMessage"
}
everything works fine...but I am trying to animate items in a list, with automatically assigned ID's. Since I want each of these to be addressable from my javascript, I believe I need to pass a parameter to my javascript. But when I use:
一切正常……但我正在尝试为列表中的项目设置动画,并自动分配 ID。因为我希望这些都可以从我的 javascript 中寻址,所以我相信我需要将一个参数传递给我的 javascript。但是当我使用:
new AjaxOptions
{
UpdateTargetId = "VoteCount" + indx,
OnSuccess = "AnimateVoteMessage(2)"
}
I get an " Sys.ArgumentUndefinedException: Value cannot be undefined." exception. Well I get that when using the debug versions of MicrosoftMvcAjax.js. When using the compressed version I get a "Microsoft JScript runtime error: 'b' is null or not an object"
我得到一个“Sys.ArgumentUndefinedException:值不能被定义。” 例外。好吧,当使用 MicrosoftMvcAjax.js 的调试版本时,我明白了。使用压缩版本时,我收到“Microsoft JScript 运行时错误:'b' 为空或不是对象”
So my question is, can I pass a parameter to my javascript function using the OnSuccessevent for a ActionLink?
所以我的问题是,我可以使用OnSuccess事件将参数传递给我的 javascript 函数ActionLink吗?
Is this the right approach? How else am I going to have one javascript function have the ability to be run on 10 items (in my case the IDs of multiple DIVs) on my page?
这是正确的方法吗?否则我将如何让一个 javascript 函数能够在我的页面上运行 10 个项目(在我的情况下是多个 DIV 的 ID)?
回答by Chase
There is a better way to do this - use the built in parameters that the OnSuccess call can be expected to pass
有一个更好的方法来做到这一点 - 使用 OnSuccess 调用可以预期传递的内置参数
the built in parameters (the ones I found so far anyway) are data, status and xhr
内置参数(到目前为止我发现的那些)是数据、状态和 xhr
data = whatever you return from the action method
数据 = 从 action 方法返回的任何内容
status = if successful this status is just a string that says "success"
status = 如果成功此状态只是一个字符串,表示“成功”
xhr = object that points to a bunch of javascript stuff that I will not be discussing...
xhr = 指向一堆我不会讨论的 javascript 内容的对象...
so you would define your javascript like this (you can leave out the arguments you don't need - since all we want is our data back from the action we will just take the data argument)
所以你可以像这样定义你的javascript(你可以省略你不需要的参数 - 因为我们想要的只是我们从动作中返回的数据,我们只需要数据参数)
function myOnSuccessFunction (data)
{
..do stuff here with the passed in data...
}
like I said before, data is whatever JSON that may be returned by the ActionResult so if your controller action method looks like this...
就像我之前说的,数据是 ActionResult 可能返回的任何 JSON,所以如果你的控制器操作方法看起来像这样......
public ActionResult MyServerAction(Guid modelId)
{
MyModel mod = new MyModel(modelId);
mod.DoStuff();
return Json(mod, JsonRequestBehavior.AllowGet);
}
you would set up your action link like this...
你会像这样设置你的动作链接......
@Ajax.ActionLink("Do Stuff", "MyServerAction", new { modelId = Model.Id }, new AjaxOptions { OnSuccess = "mySuccessScript(data);", OnFailure = "myFailureScript();", Confirm = "Are you sure you want to do stuff on the server?" })
this will create a confirmation message box asking if you want to actually invoke the action - clicking yes on the prompt will invoke the call to the controller - when the call comes back - the data argument will contain a JSON object of whatever you returned in your action method. Easy Peasy!
这将创建一个确认消息框,询问您是否要实际调用该操作 - 在提示上单击是将调用对控制器的调用 - 当调用返回时 - data 参数将包含您返回的任何内容的 JSON 对象动作方法。十分简单!
But wait! What if I want to pass another custom argument?! Simple! Just add your arguments to the front of the list...
可是等等!如果我想传递另一个自定义参数怎么办?!简单的!只需将您的参数添加到列表的前面...
instead of this...
而不是这个...
function myOnSuccessFunction (data)
{
..do stuff here with the passed in data...
}
do this (you can have more than one custom argument if you like, just keep adding them as needed)...
这样做(如果您愿意,您可以拥有多个自定义参数,只需根据需要继续添加它们)...
function myOnSuccessFunction (myCustomArg, data)
{
..do stuff here with the passed in data and custom args...
}
then in your setup - just get the argument through some client side script within your ActionLink definition... i.e.
然后在您的设置中 - 只需通过 ActionLink 定义中的一些客户端脚本获取参数...即
@Ajax.ActionLink("DoStuff", "MyServerAction", new { modelId = Model.Id }, new AjaxOptions { OnSuccess = "mySuccessScript(myClientSideArg, data);", OnFailure = "myFailureScript();", Confirm = "Are you sure you want to do stuff on the server?" })
Note that "myClientSideArg" in the OnSuccess parameter can come from wherever you need it to - just replace this text with what you need.
请注意,OnSuccess 参数中的“myClientSideArg”可以来自您需要的任何地方 - 只需将此文本替换为您需要的内容即可。
Hope That Helps!
希望有帮助!
回答by Mike
or...a bit different syntax that worked for me:
或者...对我有用的有点不同的语法:
OnSuccess = "( function() { MyFunction(arg1,arg2); } )"
回答by Magnus
There is a better way, which I believe is how Microsoft intended it: Set the AjaxOptions.OnSuccess to a function pointer, i.e. just the name of the function, no parameters. MVC will send it parameters automagically. Below is an example.
有一种更好的方法,我相信这就是 Microsoft 的意图:将 AjaxOptions.OnSuccess 设置为函数指针,即只是函数的名称,没有参数。MVC 会自动向它发送参数。下面是一个例子。
JScript parameter:
JScript 参数:
public class ObjectForJScript
{
List<int> Ids { get; set; }
}
Jscript function:
脚本函数:
function onFormSuccess(foobar){
alert(foobar);
alert(foobar.Ids);
alert(foobar.Ids[0]);
}
View code:
查看代码:
@using (Ajax.BeginForm("ControllerAction", "ControllerName",
new AjaxOptions
{
OnSuccess = "onFormSuccess" //see, just the name of our function
}))
{
@Html.Hidden("test", 2)
}
Controller code:
控制器代码:
public JsonResult ControllerAction(int test)
{
var returnObject = new ObjectForJScript
{
Ids = new List<int>{test}
};
return Json(returnObject);
}
Note that the parameter name in the JScript function doesn't matter, you don't have to call it "returnObject" even though the controller names it so.
请注意,JScript 函数中的参数名称无关紧要,即使控制器将其命名为“returnObject”,您也不必将其称为“returnObject”。
Also note that Json() conveniently turns our List into a JScript array. As long as the methods of the C# object are translatable to Json, you can call them in the JScript like I've done.
另请注意, Json() 方便地将我们的 List 转换为 JScript 数组。只要 C# 对象的方法可翻译为 Json,您就可以像我所做的那样在 JScript 中调用它们。
Finally, the controller doesn't have to return a JsonResult, it can be ActionResult (but it's usually better to have a controller action do just one thing).
最后,控制器不必返回 JsonResult,它可以是 ActionResult(但通常最好让控制器操作只做一件事)。
The above example will alert two objects (the Json and the array object which it contains), and then 2.
上面的例子将提醒两个对象(Json 和它包含的数组对象),然后是 2。
回答by Kam
You can simply do this
你可以简单地做到这一点
Razor:
剃刀:
... OnSuccess = "AnimateVoteMessage('" + YourParameter + "')"
Please note the single quotes!
请注意单引号!
JavaScript:
JavaScript:
function AnimateVoteMessage(YourParameter){
alert(YourParameter);
}
Enjoy,
享受,
回答by Alex42
Try this - it works for me:
试试这个 - 它对我有用:
OnSuccess = "new Function('MyFunction(" + myParameter + ")')"
回答by Felipe Fujiy Pessoto
Use this:
用这个:
OnSuccess = "function(){yourfunction(" + productcode + ");}"
or
或者
OnSuccess = "function(){yourfunction(this, " + productcode + ");}"
回答by silverfighter
I ran in this issue too... and did not find a way to solve it! I did a workaround (which is not nice but solved it for now... maybe until someone posts a solution here)... what I did was place a hidden field in the TargetId div and OnSuccess I called a js method which retrieves the value from the hidden field <- this could happen in your AnimateVoteMessage method ...
我也遇到了这个问题......并没有找到解决它的方法!我做了一个解决方法(这不是很好,但现在解决了......也许直到有人在这里发布解决方案)......我所做的是在 TargetId div 和 OnSuccess 中放置一个隐藏字段,我调用了一个 js 方法来检索隐藏字段中的值 <- 这可能发生在您的 AnimateVoteMessage 方法中...
回答by silverfighter
See http://www.codeproject.com/KB/ajax/Parameters-OnSuccess.aspx
见http://www.codeproject.com/KB/ajax/Parameters-OnSuccess.aspx
Basically:
基本上:
function yourCallBack(arg1,arg2) {
return function(result) { // Your old function
alert(arg1); // param will be accessible here
alert(arg2); // param will be accessible here
alert(result);// result = the response returned from Ajax
}
}

