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 &
in the url string.