How to access javascript variable within @URL.Action()

Javascriptasp.net Mvcasp.net Mvc-3Razor

Javascript Problem Overview


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>

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 - Javascript

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

var url = '@Html.Raw(@Url.Action("MethodName", "ControllerName"))' + '?id = ' + myjavascriptID

Solution 4 - Javascript

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.

Solution 5 - Javascript

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); 

Solution 6 - Javascript

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 });
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBoluView Question on Stackoverflow
Solution 1 - JavascriptBrian MainsView Answer on Stackoverflow
Solution 2 - JavascriptShawn J. MolloyView Answer on Stackoverflow
Solution 3 - JavascriptgamesView Answer on Stackoverflow
Solution 4 - JavascriptactaramView Answer on Stackoverflow
Solution 5 - JavascriptJohView Answer on Stackoverflow
Solution 6 - JavascriptxMaaView Answer on Stackoverflow