如何在url.action中使用ASP.NET MVC WebForms视图引擎中的javascript变量

时间:2022-11-30 22:33:37

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; })
});