views:

356

answers:

3

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?

A: 

work around: use javascript for submiting the form instead of submit buttons

Jack
A: 

Try this instead:

<input type="submit" value="Send" name="send" />
<input type="submit" value="Save As Draft" name="save" />

and:

public ActionResult SendEmail(string send, FormCollection form)
{
    if (!string.IsNullOrEmpty(send))
    {
        // the Send button has been clicked
    } 
    else
    {
        // the Save As Draft button has been clicked
    }
}
Darin Dimitrov
Its returning the value "Send" when clicking either of the buttons in Google Chrome, but in IE, it returns null when clicking "Save As Draft". The problem is only when using google chrome
Prasad
Are you reading my post carefully? Have you noticed the names of the buttons and the name of the parameter passed to the action?
Darin Dimitrov
yes i changed my code as per your answer, but the problem is same. I dont know whats weird with google chrome.
Prasad
+2  A: 

Show this page.

ASP.NET MVC – Multiple buttons in the same form - David Findley's Blog

Create ActionMethodSelectorAttribute inherit class.

takepara
this link also shows how to handle multiple buttons without using an ActionMethodSelectorAttribute (which is what I needed!) Thanks!
Andrew