I have a form with two submit buttons in my asp.net mvc (C#) application. When i click any submit button in Google Chrome
, by default the value of submit button is the first submit button's value.
Here is the html:
<input type="submit" value="Send" name="SendEmail" />
<input type="submit" value="Save As Draft" name="SendEmail" />
<input type="button" value="Cancel" />
When i click the Save As Draft
button, in the action of the controller, it gets "Send" as the value for SendEmail
.
Here is the action:
public ActionResult SendEmail(string SendEmail, FormCollection form)
{
if(SendEmail == "Send")
{
//Send Email
}
else
{
//Save as draft
}
return RedirectToAction("SendEmailSuccess");
}
When i get the value from FormCollection, it shows "Send". i.e. form["SendEmail"]
gives Send
What may be the problem or work around i need to do to get the actual value of the clicked submit button?
yes, multiple submit buttons can include in the html form. One simple example is given below.
Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.
Having multiple submit buttons and handling them through PHP is just a matter of checking the the name of the button with the corresponding value of the button using conditional statements. In this article I'll use both elseif ladder and switch case statement in PHP to handle multiple submit buttons in a form.
Show this page.
ASP.NET MVC – Multiple buttons in the same form - David Findley's Blog
Create ActionMethodSelectorAttribute inherit class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With