Use a regular HTML button to invoke a controller in ASP.NET MVC2?

I want to use a regular HTML button to call Controller in ASP.NET MVC2 instead of ActionLink, is this possible?

+3
source share
3 answers

Usually with a button, you will use javascript to call an action or wrap it in a form (or both).

<% Html.BeginForm( "action", "controller", new { id = Model.ID } )
   { %>
     <button type="submit">Go</button>
<% } %>

<script type="text/javascript">
    $(function() {
         $('button').click( function() {
              var form = $('form');
              $.post( form.attr('action'), form.serialize(), function(msg) {
                    alert(msg);
              });
              return false; // don't really submit the form
         });
    });
</script>
+3
source

Of course, just use Url.Action()to display the action url.

<a href="<%= Url.Action("Action") %>">BLA BLA</a>

or

<button onclick="javascript: window.location = '<%= Ajax.JavascriptEncode(Url.Action("Action")) %> '">Click me</button>
+4
source
<form method="get" action="YourUrl">
    <button type="submit">Click me</button>
    <input type="submit" value="Or click me" />
</form>

, <%= Url.Action(...) %> URL action .

javascript, , . button , input, , , . .

+1

Source: https://habr.com/ru/post/1776447/


All Articles