1

I am able to redirect the user with code in a function like this:

 window.location.href = "@Url.Action("ShippingSummary", "Shipping", new { id = 2 })"

I have the id in a variable but I need to assign it to the ActionLink dynamically. Something like this

var ID = 2;
window.location.href = "@Url.Action("ShippingSummary", "Shipping", new { id = ID })"

I get an error that ID is not defined. I have tried to create the actionlink as a string first but that didn't work. How can I get this to work?

1 Answer 1

4

In this instance, you are mixing Javascript and C#. The Javascript will be executed in the browser, but the C# will be executed on the server, so you can't mix them like this.

Instead, you should do:

var ID = 2;
window.location.href = "@Url.Action("ShippingSummary", "Shipping")" + "?id=" + ID;

In this instance, Razor will emit the first part of the Controller Action route, and then you can add the needed query string parameter to the path.

Sign up to request clarification or add additional context in comments.

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.