Reputation: 10311
I want to redirect the user to a url in a javascript function.
editClick: function () {
var myid = 1;
location.href = '<%= Url.Action("ActionName", "ControllerName", new { id = myid})%>';
};
But 'myId' does not exist in the current context. How do I use a js variable in Url.Action?
Upvotes: 0
Views: 1916
Reputation: 1483
An approach that I've taken in the past is to generate the link in the view like so:
<div class='clickmaster'>
<div class='clicker' id='@Url.Action("ActionName", "ControllerName", new { id = Model.Id })'>
Show Something Here
</div>
<div class='clicker' id='@Url.Action("ActionName", "ControllerName", new { id = Model.Id })'>
Show Something Here
</div>
</div>
Then in the javascript I defined a function like this:
$(function() {
$('.clickmaster').on('click', '.clicker' , function() { location.href = this.id; })
});
Upvotes: 1