How do I redirect a user when a button is clicked?
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") %>';" />
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")';
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>
if using JQuery, you can do this :
<script type="text/javascript">
$('#buttonid').click(function () {
document.location = '@Url.Action("ActionName","ControllerName")';
});
</script>
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")'" />