Reputation: 2055
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?
Upvotes: 1
Views: 4025
Reputation: 5298
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.
Upvotes: 4