Can I pass a parameter with the OnSuccess event in a Ajax.ActionLink

Viewed 39654

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:

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"

So my question is, can I pass a parameter to my javascript function using the OnSuccess event for a 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?

9 Answers

or...a bit different syntax that worked for me:

OnSuccess = "( function() { MyFunction(arg1,arg2); } )"

Try this - it works for me:

OnSuccess = "new Function('MyFunction(" + myParameter + ")')"

Use this:

OnSuccess = "function(){yourfunction(" + productcode + ");}"

or

OnSuccess = "function(){yourfunction(this, " + productcode + ");}"

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

Simply, at AjaxOptions, use the following:

OnSuccess = "onSuccessFunction(data, 'Arg1');"

Then at your function, you will get the new value as:

function onSuccessFunction(result, myNewArg) {
   //Prints Arg1
   Console.Write(myNewArg)
}
Related