Search code examples
c#asp.net-mvchtml-helperhtml.actionlink

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


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, tested, and functioning. I can get to them by typing the url.

I looked for steps explaining how to wire up the onclick event of the button but I'm new to MVC and kinda lost at this point.

Thanks!


Solution

  • 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") %>';" />