MVC button to go to action

Possible duplicate:
Mvc Html.ActionButton

C ASP.NET MVC 3I know how easy it is to create a link to an action method, but I would like to know how you press the button (when pressed) to call a specific action method?

+5
source share
4 answers

Sachin,

If you are using jquery (which you did not mention, but I will show that it is pretty standard with mvc), you should do the following:

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

Of course, you probably want to use ajax, but this is a basic example.

+22
source

You can use html <form>:

@using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Get))
{
    <input type="submit" value="Click me" />
}
+14
source

, GET , javascript, .

. Ajax

javascript,

$(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;
    });
});

href

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

, , , URL-,

+4

? , MVC.

<INPUT TYPE="BUTTON" VALUE="Home Page" ONCLICK="window.location.href='/Controller/Action'"> 
+4

All Articles