MVC button click to action

Sachin Kainth picture Sachin Kainth · May 25, 2012 · Viewed 68.3k times · Source

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?

Answer

jim tollan picture jim tollan · May 25, 2012

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.