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.
You could use normal buttons(non submit). Use javascript to rewrite (at an 'onclick' event) the form's 'action' attribute to something you want and then submit it. Generate the button using a custom helper(create a file "Helper.cshtml" inside the App_Code folder, at the root of your project) .
And then use it as:
Inside your form.
Didn't see an answer using tag helpers (Core MVC), so here it goes (for a delete action):
On HTML:
On Controller:
Don't forget to use
[Route("[controller]")]
BEFORE the class declaration - on controller.This is what worked for me.
Snippet :
Might help some one who wants to have 2 different action methods instead of one method using selectors or using client scripts .
You could also try this:
Then in your default function you call the functions you want:
This elegant solution works for number of submit buttons:
And in your controllers' action method accept it as a parameter.
Try wrapping each button in it's own form in your view.