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>
You can pass in the variables to any link as shown below...
You can build the JavaScript code in C# this way:
Here is the code from the question rewritten using this technique:
You can replace url like code below
You can't. JavaScript doesn't execute when generating the action URL. What you can do, is do something like this:
HTH.
I do something fairly similar, but less verbose:
We can do this because of routing, and ultimately Url.Action with route dictionary parameters translates into a URI that looks like:
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."
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: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:I added
Html.Raw
in my second example in order to avoid having&
in the url string.