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

Bolu picture Bolu · Jun 23, 2011 · Viewed 94.5k times · Source

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>

Answer

Brian Mains picture Brian Mains · Jun 23, 2011

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