ASP.NET MVC AjaxOptions, OnBegin / Onsuccess / OnFailure

If you have a Ajax.Actionlink and you want to specify a OnBegin with a parameter you need to use the following syntax:

<%=Ajax.ActionLink("text", "Action", "Controller", new { param = someVar }, new AjaxOptions() { OnBegin = string.Format("new Function(‘DoSomeThing({0})’)", index),  HttpMethod = "POST" })%>

Where DoSomeThing is just a function that takes a string as parameter.

Update: added quotes around the real (inner) function, otherwise it’s executed instead of passed on at the creation of the object.

5 thoughts on “ASP.NET MVC AjaxOptions, OnBegin / Onsuccess / OnFailure

  1. Pingback: ASP.NET MVC Archived Buzz, Page 1

  2. Why does the string.Format not have a {0} in it? are the tick marks ` intentional or are they mean to be single quotes?

  3. Those backticks are single quotes, I don’t know why my blog makes them backticks. And indeed, I forgot the {0}, thanks for that. It’s because I refactored my function to something more generic. I will fix it soon :)

  4. It’s cool! It was hard to find out my mistake. Your post help me. Use it not only for OnBegin, use it for OnSuccess, OnComplete and others too.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>