I have found countless examples and posts about passing “code behind” variables to Javascript, but I’m wondering if there is a way to do the opposite.
In my view, I’m trying to dynamically create a Url using an ASP helper class and a variable from javascript. What I would like to do is something like:
var url = '@Url.Action("' + actionname + '", "controller")';
… where actionname is predetermined by some other logic. This won’t work because I can’t break up the inline code like that.
Anybody else tried to do something like this before or have any ideas?
Thanks!
If
actionnameis a javascript variable, you cannot do this: the@Url.Actionmethod is executed on the server side, before being returned to the client. Thus, the javascript, which is executed on the client, cannot send information into the@Url.Actioncall.If this is still the case, you could just pass the
actionnameas an arg to an Action that determines where to route it.