I want to redirect the user to a url in a javascript function.
我想将用户重定向到javascript函数中的url。
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?
但是'myId'在当前的背景下并不存在。如何在Url.Action中使用js变量?
1 个解决方案
#1
1
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:
然后在javascript中我定义了一个这样的函数:
$(function() {
$('.clickmaster').on('click', '.clicker' , function() { location.href = this.id; })
});
#1
1
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:
然后在javascript中我定义了一个这样的函数:
$(function() {
$('.clickmaster').on('click', '.clicker' , function() { location.href = this.id; })
});