How to access javascript variable within @URL.Action()
Solution 1:
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 });
}
Solution 2:
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."
Solution 3:
You can pass in the variables to any link as shown below...
var url = '@Html.Raw(@Url.Action("MethodName", "ControllerName"))' + '?id = ' + myjavascriptID