简体   繁体   中英

Submit button redirect to another view ASP.NET MVC

I want to create submit button, which redirects me to to another view when I click on Accept.

I tried this:

<div class="form-group">
      <div class="col-md-offset-2 col-md-10">
           <input type="submit" value="Accept" onclick="@("window.location.href='" + @Url.Action("Index", "kupec") + "'");" class="btn btn-default"/>
      </div>
</div>

And this:

<input id="#redirect" type="submit" value="Accept" class="btn btn-default" />

with JavaScript:

$("#redirect").click(function () {
    document.location = '@Url.Action("Index","Dokument")';
});

However, neither of these two is working properly. What am I doing wrong?

You are binding the click event to an element with Id redirect . But your button id is not redirect ,but #redirect ( extra # prefix). remove that and it should work.

<input id="redirect" type="submit" value="Accept" class="btn btn-default" />

Also it might be a good idea to wrap your event handlers in the document ready event. If your button is inside a form element, it is a good idea to prevent the default form submit behavior as well.( If you really don't want to submit the form, you should not keep it inside the form ! :) )

$(function(){
  $("#redirect").click(function (e) {
     e.preventDefault();
     document.location = '@Url.Action("Index","Dokument")';
  });
});

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM