views:

554

answers:

4

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 controler's action method.

Thank you,

Aaron

+2  A: 

The HTML <button> element can only postback to the form containing it.

Therefore, you need to make a form that POSTs to the action, then put a <button> or <input type="submit" /> in the form.

SLaks
A: 

Just to add on a little, in your view you can do something like this:

<% using (Html.BeginForm()) { %>
  <fieldset>
    <input type=submit" value="Submit"/>
  </fieldset>
<% } %>

Here is a good tutorial that I recently went through which helped me a bunch with MVC: NerdDinner

Jisaak
You don't need the `<fieldset>` tag.
SLaks
+1  A: 

You can use Url.Action to specify generate the url to a controller action, so you could use either of the following:

<form method="post" action="<%: Url.Action("About", "Home") %>">
   <input type="submit" value="Click me to go to /Home/About" />
</form>

or:

<form action="#">
  <input type="submit" onclick="parent.location='<%: Url.Action("About", "Home") %>';return false;" value="Click me to go to /Home/About" />
  <input type="submit" onclick="parent.location='<%: Url.Action("Register", "Account") %>';return false;" value="Click me to go to /Account/Register" />
</form>
Jon Galloway
+1  A: 

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") %>'" />
Cheddar
I used this suggestion because it doesn't require a form. Thank you!
Aaron Salazar