How do I redirect a user when a button is clicked?

前端 未结 10 758
情话喂你
情话喂你 2020-12-13 05:30

I have a view with a button. When the user clicks the button I want them redirected to a data entry view. How do I accomplish this? I should mention the views are created, t

相关标签:
10条回答
  • 2020-12-13 06:13
    $("#yourbuttonid").click(function(){ document.location = "<%= Url.Action("Youraction") %>";})
    
    0 讨论(0)
  • 2020-12-13 06:13

    RIGHT ANSWER HERE: Answers above are correct (for some of them) but let's make this simple -- with one tag.

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

    Here's what your solution should look like using HTML:

    < 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 讨论(0)
  • 2020-12-13 06:15

    Just as an addition to the other answers, here is the razor engine syntax:

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

    or

    window.location.href = '@Url.Action("actionName", "controllerName")';
    
    0 讨论(0)
  • 2020-12-13 06:15

    It has been my experience that ASP MVC really does not like traditional use of button so much. Instead I use:

      <input type="button" class="addYourCSSClassHere" value="WordsOnButton" onclick="window.location= '@Url.Action( "ActionInControllerHere", "ControllerNameHere")'" />
    
    0 讨论(0)
  • 2020-12-13 06:17

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

    <a href="/Controller/View" class="Button">Text</a>
    
    0 讨论(0)
  • 2020-12-13 06:32

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

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

    For posting you could use a 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") %>';" />
    
    0 讨论(0)
提交回复
热议问题