A Razor view has 3 buttons inside a form. All button's actions will need form values which are basically values coming input fields.
Every time I click any of buttons it redirected me to default action. Can you please guide how I can submit form to different actions based on button press ?
I really appreciate your time, guidance and help.
Simplest way is to use the html5
FormAction
andFormMethod
There are many other ways which we can use, see this article ASP.Net MVC multiple submit button use in different ways
The cleanest solution I've found is as follows:
This example is to perform two very different actions; the basic premise is to use the value to pass data to the action.
In your view:
Then in your action:
This code should be easy to alter in order to achieve variations along the theme, e.g. change the button's name to be the same, then you only need one parameter on the action etc, as can be seen below:
In your view:
Then in your action:
In case you're using pure razor, i.e. no MVC controller:
Clicking each of the buttons should render out Hello and World.
in the view
in the action
This answer will show you that how to work in asp.net with razor, and to control multiple submit button event. Lets for example we have two button, one button will redirect us to "PageA.cshtml" and other will redirect us to "PageB.cshtml".
You can use JS + Ajax. For example, if you have any button you can say it what it must do on click event. Here the code:
Here your button in html: in the script section, you need to use this code (This section should be at the end of the document):
And finally, you need to add ajax function (In another script section, which should be placed at the begining of the document):