Invoke a specific controller action by clicking the HTML button (Do Not Send or Create Message button) Asp.net MVC

When we click the "Submit Form" button, the action of the controller that has the HTTPPost attribute is called, but what if I want to call or execute the action when I click the regular HTML button Although the following articles

http://www.codeproject.com/Tips/198477/Calling-a-MVC-Controller-and-Action-Method-using-H

HTML button calling MVC and action method

talks about the approach, but both of them use the name of the controller in the view. Therefore, view must know about the controller. I am looking for an answer that should not know about the controller. because the views must be independent of the controller, the views should not know about the controller So, if you know the answer, answer

+8
c # asp.net-mvc asp.net-mvc-3 asp.net-mvc-4
source share
3 answers

any form that directs the user to the URL created

<a href='@Url.Action("{action}", "{controller}")'> click me </a> 

or

 @using(BeginForm("{action}", "{controller}") 

will do what you want.

It could be with

  • The form
  • button

This destination is important. View does not know anything about the action or controller. The assistant does.

+16
source share

To execute the MVC action from the client side (i.e. from the view), you need to click the URL (using any verb: get, post, put, etc.). Therefore, to execute a form of an action of a view, you will need to find out the URL of this action, by default this URL is directly mapped to controllername/actionname , but you can redefine it if you want to create more abstraction between the view and the controller.

Given this, your button just needs to be a link to a url or linked to js in order to execute an Ajax HTTP request.

Hope this helps.

0
source share

You cannot have 2 actions on the same controller with the same name and with the same HTTP verb. What you ask does not make sense. You can invoke the same controller action as the one that displayed the view, without specifying the name of the action and controller. The reason Html.BeginForm() works without specifying an action name and controller is because the form sends a POST request to the server, and you can distinguish between the two actions.

0
source share

All Articles