How to use a javascript variable in url.action with ASP.NET MVC WebForms view engine

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?

Answers


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

Need Your Help

Asp.Net MVC - Change error class name

asp.net-mvc validation xval

When I do field validation in Asp.Net MVC, it's generated the following error css classes :

How to resolve TemplateSyntaxError: Could not parse the remainder error?

python django python-2.7 templates django-models

I'm getting a "TemplateSyntaxError: Could not parse the remainder:" here: https://docs.djangoproject.com/en/1.7/intro/tutorial05/

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.