How to redirect a user when a button is clicked?

I have a view with a button. When the user clicks the button, I want them to be redirected to the data entry view. How to do it? I must mention that opinions are created, tested and function. I can reach them by typing the URL.

I was looking for steps explaining how to connect the onclick event to a button, but I am new to MVC and lost at this point.

Thank!

+53
asp.net-mvc
Jul 02 '10 at 7:28
source share
8 answers

It depends on what you mean by the button. If this is a link:

<%= Html.ActionLink("some text", "actionName", "controllerName") %> 

For publication, you can use the form:

 <% using(Html.BeginForm("actionName", "controllerName")) { %> <input type="submit" value="Some text" /> <% } %> 

And finally, if you have a button:

 <input type="button" value="Some text" onclick="window.location.href='<%= Url.Action("actionName", "controllerName") %>';" /> 
+88
Jul 02 '10 at 19:30
source share

As a complement to the other answers, here is the syntax of the razor mechanism:

 <input type="button" value="Some text" onclick="@("window.location.href='" + @Url.Action("actionName", "controllerName") + "'");" /> 

or

 window.location.href = '@Url.Action("actionName", "controllerName")'; 
+35
Jan 15 '14 at 16:30
source share

if you are using jQuery you can do this:

 <script type="text/javascript"> $('#buttonid').click(function () { document.location = '@Url.Action("ActionName","ControllerName")'; }); </script> 
+8
Sep 15 '15 at 9:12
source share

If, like me, you don't like relying on JavaScript for button links. You can also use anchor and style it like your buttons using CSS.

 <a href="/Controller/View" class="Button">Text</a> 
+6
Jul 02 '10 at 19:40
source share

Or, if none of the above actions work, you can use the following approach, since it worked for me.

Imagine this is your button

 <button class="btn" onclick="NavigateToPdf(${Id});"></button> 

I got a value for $ {Id} populated using jquery templates. You can use whatever suits you. In the next function, I set window.location.href to be the name of the controller, then the name of the action, and then the finally parameter. I can successfully navigate.

 function NavigateToPdf(id) { window.location.href = "Link/Pdf/" + id; } 

Hope this helps.

+3
Sep 16 '12 at 21:18
source share

You can easily wrap the tag of your button with a tag. Using Url.Action () HTML Helper , you can go from one page to another.

 <a href='@Url.Action("YourAction", "YourController")'> <input type='button' value='Dummy Button' /> </a> 

If you want to navigate using the jaascript onclick () function, use

 <input type='button' value='Dummy Button' onclick='window.location = "@Url.Action("YourAction", "YourController")";' /> 
+2
Apr 12 '18 at 8:00
source share
 $("#yourbuttonid").click(function(){ document.location = "<%= Url.Action("Youraction") %>";}) 
0
Jul 02 '10 at 7:32
source share

DIRECT ANSWER HERE: The answers above are correct (for some of them), but let's do it simply - with one tag.

I prefer to use input tags, but you can use a button tag

Here's what your HTML solution should look like:

 < input type="button" class="btn btn-info" onclick='window.location.href = "@Url.Action("Index", "ReviewPendingApprovals", routeValues: null)"'/> // can omit or modify routeValues to your liking 
0
Jul 26 '19 at 19:20
source share



All Articles