1

How do you make use of a jquery variable within Url.Action within jquery?

var id = "10";
window.location.href = '@Url.Action("MyAction", "MyController", new { id })';

Thanks

1
  • The question is, does your id need to be set client side? Or can that value of "10" also be set server-side using Razor (e.g. like var id = "@(myServerValue)" and @Url.Action("MyAction", "MyController", new { Id = myServerValue })) Commented Feb 7, 2014 at 11:56

5 Answers 5

3

try, replace function by placing a token:

var id = "10";
var actionUrl = "@Url.Action("MyAction","MyController", new { Id= "TabId" })";
window.location.href = actionUrl.replace("TabId", id);
Sign up to request clarification or add additional context in comments.

9 Comments

+1: Token replacement is a good workaround if the value needs to come from client-side. This allows for more complex URLs than simply appending the parameter to the URL.
@TrueBlueAussie What if you don't want to send any value, i.e. you have a nullable field. This approach won't work, the best you can do is send an empty string, e.g. Id=1&SomeNullableField=&SomeOtherField=2
@Rui: that seems unlikely in this example, but a regex replace like /[?&]Id=TabId/ etc instead would do that (if you want nulls). The problem with your example below is that it does handle other parameters (which happen frequently in using Action). You then need to handle ? and & cases.
@TrueBlueAussie Handling ? and & is not really an issue if you use jQuery, for example $.param({id: 10, someOtherParameter:20}) -> id=10&someOtherParameter=20
@TrueBlueAussie I've changed my answer to reflect this
|
0

If I understand you question correctly.. To achieve this I would make an Ajax call with your jQuery to a Razor page passing your jQuery parameters as GET/POST then retrieve the GET/POST on the Razor page and pass something back via Ajax.

Comments

0

Jquery,javascript statements and variables are evaluated in browser. Razor is evaluated in web server. So you cannot do what you mean. But if you set value in a razor variable, you can use it on both razor and javascript code.

Comments

0

You can't use a jQuery variable in Razor.
The reason why you can't use a javascript variable in razor is that razor runs in the server. Javascript runs in the client.

You could however store that url in a javascript variable and then use it to build the specific url you need, i.e.:

var url = '@Url.Action("MyAction", "MyController")';

window.location.href = url + "?id=" + id;

If you need to pass several parameters and you don't want to handle creating a string with ?param1=X&param2=Y etc you can use jQuery's param method, i.e.:

window.location.href = url + "?" + $.param({id:10, param2:"hello"});

$.param({id:10, param2:"hello"}) returns id=10&param2=hello

1 Comment

The only reason I prefer the token replacement answer is I personally hate the maintenance problems caused by string concatenation in code. Also, the question asked was a very simple example, so only needed a very simple solution.
0

Just to complete the set of possible answers, if your variable value is known server-side (you have not clarified that point) you can also inject Razor values into your javascript:

e.g.

var id = "@(myServerValue)";
window.location.href = @Url.Action("MyAction", "MyController", new { Id = myServerValue })) 

If the value is only known client-side, you can use a client-side replace as Zaheer Ahmed and Rui suggested.

If you need to handle null cases, and want to remove the extra param, I would go as far as using a regex to match the replacement:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.