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?
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; })
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.