embedding javascript variable within razor syntax

I have a method that looks like this

function endcall_click(leadid) {
    document.location = '@Url.Action("index","dispo",new{id=leadid})/';
}

Of course it doesn't work because it treats "leadid" as a server side variable but I want to inject the javascript variable passed into the method.

I tried wrapping lead id in but that didn't work.

function endcall_click(leadid) {
    document.location = '@Url.Action("index","dispo",new{id="<text>leadid</text>"})/';
}

Any ideas?

Answers


You can't inject javascript variable to a script that is evaluated at the server simply because at the moment this script executes and generates the output this variable hasn't yet come to existence. The only way to achieve this is to manipulate the resulting string:

function endcall_click(leadid) {
    document.location = '@Url.Action("index", "dispo")/' + leadid;
}

The drawback is that this assumes manipulating the routes in javascript and if you decide to change them on the server the code might break.


Need Your Help

Robust atan(y,x) on GLSL for converting XY coordinate to angle

c++ glsl coordinates atan2 numerical-stability

In GLSL (specifically 3.00 that I'm using), there are two versions of

Determine empty column with Nothing, = Nothing or IsNullOrEmpty

sql vb.net nothing isnullorempty

When you want to determine if a database column is empty (i.e not null but there is not any value in it) what are the differences between the below options: