views:

741

answers:

1

When using the Ajax.BeginForm() helper in ASP.Net MVC, I can pass options with names of different functions, for example one to run OnBegin, one for OnSuccess etc. How do these work "under the hood"?

The reason I'm asking is that I'm extending this to provide a jQuery based alternative, and I need to figure out how to get from having the method names specified in a json string to actually calling them.

The AjaxOptions class has a method for serializing, in which it wraps the method name in the following way (in this example, the AjaxOptions OnSuccess property is set to "mySuccessFunction(p)":

onSuccess: Function.createDelegate(this, mySuccessFunction(p))

I have produced my own JQueryAjaxOptions class, which serializes in the same way (but with some extra options available). So asically, what I have available in my method for posting the form is the above property as part of a json object, and I need to be able to call the function.

How can I accomplish this?

+1  A: 

I found the answer to my problem:

What it really boiled down to, was executing a line of code contained in a string variable. This can easily be done using the 'eval(x)' syntax. The following example works for the onSuccess property:

eval(settings.onSuccess)

Sometimes you just need to think one more step backwards to realize what you're trying to do, and how easy it is =)

Tomas Lycken