HTML button calling an MVC Controller and Action method

Aaron Salazar picture Aaron Salazar · Mar 23, 2010 · Viewed 770.7k times · Source

I know this isn't right, but for the sake of illustration I'd like to do something like this:

<%= Html.Button("Action", "Controller") %>

My goal is to make an HTML button that will call my MVC controller's action method.

Answer

Cheddar picture Cheddar · Mar 24, 2010

No need to use a form at all unless you want to post to the action. An input button (not submit) will do the trick.

  <input type="button"
         value="Go Somewhere Else"
         onclick="location.href='<%: Url.Action("Action", "Controller") %>'" />