Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a specific action of a controller on the click of HTML button(Not Submit or Form's post Button) Asp.net MVC

When we click a Form's submit button, then action of the controller which is having HTTPPost attribute is called but what if i want to call or perform an action when a normal HTML button is clicked Although following articles

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

HTML button calling an MVC Controller and Action method

tells the approach but both of these are using controller name in view, So view has to know about controller, I am looking for an answer that view wont have to know about controller. because views has to be Independent from Controller, Views should not know about controller So, if you know the answer then please reply

like image 467
Yogesh Avatar asked Mar 01 '13 13:03

Yogesh


People also ask

How can call POST action method on button click in MVC?

click(function () { //On click of your button var property1 = $('#property1Id'). val(); //Get the values from the page you want to post var property2 = $('#property2Id').


1 Answers

any form that directs your user to url created by

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

or

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

will do what you want.

That can be with a

  • form
  • button link

It's the destination that matters. The View does not "know" anything about the action or controller. The helper does.

like image 87
Dave Alperovich Avatar answered Oct 12 '22 02:10

Dave Alperovich