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

104,010

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

Solution 4

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

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); 
Share:
104,010

Related videos on Youtube

Bolu
Author by

Bolu

#SOreadytohelp

Updated on July 05, 2022

Comments

  • Bolu
    Bolu almost 2 years

    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>
    
  • Bolu
    Bolu almost 13 years
    Thank you for your answer, it works with one small modification: instead of link.href I need to use link . Thanks very much!
  • Nic
    Nic about 10 years
    What if id will be string like "Hello world" ?
  • Brian Mains
    Brian Mains about 10 years
    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
  • juFo
    juFo almost 9 years
    is there no cleaner solution?
  • Kate
    Kate about 8 years
    It will break RoutAttribute configuration :(((
  • alcohol is evil
    alcohol is evil almost 8 years
    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.
  • Shaiju T
    Shaiju T over 7 years
    remove capital URL prefixed before @URL.Action to @Url.Action as it shows compile time error
  • David Létourneau
    David Létourneau about 7 years
    Add your answer with this one and got the perfect answer: stackoverflow.com/a/36981170/453142
  • Grenville
    Grenville about 6 years
    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.
  • Mr Nellinger
    Mr Nellinger about 5 years
    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.