79

How can I access JavaScript value inside @URL.Action()? something like:

<script type="text/javascript">
function name(myjavascriptID)
{
     jQuery("#list_d").jqGrid('setGridParam', { url: '@URL.Action("download file", "download", new { id = <myjavascriptID> })', page: 1 });

}
</script>
tereško
  • 58,060
  • 25
  • 98
  • 150
Bolu
  • 8,696
  • 4
  • 38
  • 70

6 Answers6

149

You can't. JavaScript doesn't execute when generating the action URL. What you can do, is do something like this:

function name(myjavascriptID)    {
     var link = '@Url.Action("download file", "download", new { id = "-1" })';
     link = link.replace("-1", myjavascriptID);

     jQuery("#list_d").jqGrid('setGridParam', { url: link, page: 1 });
}
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • Thank you for your answer, it works with one small modification: instead of `link.href` I need to use `link` . Thanks very much! – Bolu Jun 23 '11 at 16:04
  • What if id will be string like "Hello world" ? – Nic Apr 03 '14 at 10:29
  • The idea is for the -1 to be something uniquely identifiable in the string, for you to replace with whatever you want. You would need to make sure Hello World is encoded, like Hello+World. But yes, replace it with whatever you want – Brian Mains Apr 03 '14 at 13:38
  • 2
    is there no cleaner solution? – juFo May 12 '15 at 12:30
  • Great answer. Now I don't have to worry about changes in routing configuration. I use "{id}" string instead of "-1", so it looks more natural to me and I don't have to worry about using -1 value for any other variable in the future, but unfortunately in replace function it has to be converted by Url.Encode and also there's a difference in letters case in those strings. – alcohol is evil Jul 31 '16 at 10:19
  • 1
    remove capital URL prefixed before `@URL.Action` to `@Url.Action` as it shows compile time error – Shaiju T Dec 29 '16 at 06:54
  • Hadn't thought of this. It's a good shout as I would never have -1 as a valid Id, yet it's a valid int for my route. Only issue is if there 'is' a valid -1 elsewhere in the route. Still, best solution I've yet seen. – Grenville Mar 09 '18 at 14:23
  • Good to note you can do this as many times as you want for multiple params. Just use -2, -3 and so on. This trick really helped me out at work today. Thanks. – Mr Nellinger Mar 14 '19 at 16:03
20

I do something fairly similar, but less verbose:

var myUrl = '@Url.Action("Solution","Partner")/' + myjavascriptID;
$.ajax.load(myUrl); // or whatever

We can do this because of routing, and ultimately Url.Action with route dictionary parameters translates into a URI that looks like:

http://localhost:41215/Partner/Solution?myJavascriptID=7

Just a second choice, because as a wise old man once said "It is our choices, Harry, that show what we truly are, far more than our abilities."

Shawn J. Molloy
  • 2,457
  • 5
  • 41
  • 59
13

You can pass in the variables to any link as shown below...

var url = '@Html.Raw(@Url.Action("MethodName", "ControllerName"))' + '?id = ' + myjavascriptID
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
games
  • 131
  • 1
  • 3
2

In the same vein as Brian Mains's answer, you could format your url string instead of replacing -1 with your variable, that is if like me, you judge it is better to read. The following answer assumes that you've modified String's prototype as suggested in this answer:

var url = unescape('@Url.Action("download file", "download", new { id = "{0}" })').format(myjavascriptID);

The unescape call is necessary if you want to decode your {0}. I like this alternative, because it makes it easier to have multiple parameters from JS variables. For instance:

var url = unescape('@Html.Raw(Url.Action("Action", "Controller", new { id = "{0}", name = "{1}" }))').format(myID, myName);

I added Html.Raw in my second example in order to avoid having &amp in the url string.

Community
  • 1
  • 1
actaram
  • 2,038
  • 4
  • 28
  • 49
1

You can replace url like code below

var jsUrl = '@Url.Action("action", "controller")'; // ## is the token
var getUrl = jsUrl.replace('action', action).replace('controller', controller)  
$("#RenderPartial").load(getUrl); 
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Joh
  • 11
  • 1
0

You can build the JavaScript code in C# this way:

function fun(jsId) {
  var aTag = '<a href="@Html.Raw(@Url.Action("ActionName", "ControllerName", new { objectId = "xxx01xxx" }).Replace("xxx01xxx", "' + jsId + '"))">LINK</a>';
}

Here is the code from the question rewritten using this technique:

function name(myjavascriptID) {
  jQuery("#list_d").jqGrid('setGridParam', { url: '@Html.Raw(@URL.Action("download file", "download", new { id = "XXXmyjavascriptIDXXX" }).Replace("XXXmyjavascriptIDXXX", "' + myjavascriptID + '"))', page: 1 });
}
xMaa
  • 132
  • 6