5

Possible Duplicate:
Mvc Html.ActionButton

With ASP.NET MVC 3 I know how to create a link to an action method very easily but what I'd like to know is how do you make a button (when clicked) call a particular action method?

Community
  • 1
  • 1
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304

4 Answers4

21

Sachin,

If you're using jquery (which you don't mention but I'll show as it's fairly standard with mvc), you'd do the following:

$('#buttonId').click(function(){
   document.location = '@Url.Action("MyAction","MyController")';
});

of course, you'd probably want to use ajax, but this is a basic example.

jim tollan
  • 22,305
  • 4
  • 49
  • 63
15

You could use an html <form>:

@using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Get))
{
    <input type="submit" value="Click me" />
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
4

As well as Darin's answer about using a form GET method, you can also call javascript functions that will then in turn call an action.

You can intercept the button click event when clicked and run your own code. That code can call an action asynchronously using Ajax or just simply navigate to an action method

Here's sample javascript that intercepts the button click event

$(document).ready(function () {

    myButton.onclick = function (event) {
        // in here you can call an ajax method or just navigate to an action 
        return false;
    }

    // or using jQuery
    $('#myButton').click(function (e) {
        // do whatever here
        e.preventDefault;
    });
});

Or you can intercept a button that has an href attribute

$(function () {
     $("#myButton").click(function () {
         var href = $(this).attr("href");
         var route = href + "?paramName=" + $('#SomeValue').val();
         $(this).attr("href", route);
     });
});

This adds parameter information that you may have stored in another input on the page and appends it to the Url and then navigates to the action

CD Smith
  • 6,597
  • 7
  • 40
  • 66
  • Why use javascript when HTML already has all that's necessary? Writing semantic markup is always better than javascript. – Darin Dimitrov May 25 '12 at 11:54
  • Yes, but the OP was asking about clicking a button to call an action, that action might not necessarily be a Post of a form, it could just be for navigation or an simple ajax request.. – CD Smith May 25 '12 at 11:56
  • Who said that a form can only do POST? You could perfectly fine use the GET verb. – Darin Dimitrov May 25 '12 at 11:56
  • Yes, I used a form because I think that this is the semantically correct way to achieve that. – Darin Dimitrov May 25 '12 at 11:57
  • I see that, just giving other options. There are plenty of other reasons to call an action that wouldn't require the use of a form. Maybe he has a button within a form that already does a post and he wants that button to call an action, the nested form then wouldn't work. There are always options :-) – CD Smith May 25 '12 at 11:58
4

How do you do it? The same way you would if not using MVC.

<INPUT TYPE="BUTTON" VALUE="Home Page" ONCLICK="window.location.href='/Controller/Action'"> 
David
  • 49
  • 1