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