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"?
在ASP.Net MVC中使用Ajax.BeginForm()帮助程序时,我可以传递具有不同函数名称的选项,例如一个用于运行OnBegin,一个用于OnSuccess等。这些工作如何“在引擎盖下”?
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.
我问的原因是我正在扩展它以提供基于JQuery的替代方案,我需要弄清楚如何从JSON字符串中指定的方法名称实际调用它们。
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)
:
AjaxOptions类有一个序列化方法,它以下列方式包装方法名称(在本例中,AjaxOptions OnSuccess属性设置为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.
我已经创建了自己的JQueryAjaxOptions类,它以相同的方式序列化(但有一些额外的选项可用)。因此,我在发布表单的方法中可用的是上面的属性作为json对象的一部分,我需要能够调用该函数。
How can I accomplish this?
我怎么能做到这一点?
1 个解决方案
#1
1
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(x)'语法轻松完成。以下示例适用于onSuccess属性:
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 =)
有时你只需要再思考一步,以实现你想要做的事情,以及它是多么容易=)
#1
1
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(x)'语法轻松完成。以下示例适用于onSuccess属性:
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 =)
有时你只需要再思考一步,以实现你想要做的事情,以及它是多么容易=)