1

I am developing my first MVC app. It's like a small blog that users can post news item. Here, I want to have two submit buttons and want to handle them differently on the controller. For example, one for publishing and one for saving to publish later. The difference will be just a Boolean in the database but I don't think I can have two submit buttons on a view or have two ActionResult Create on the controller. I know I could use a checkbox that would make it simpler but I don't like the usability with checkbox. What will be the best way to handle this? Thanks.

enter image description here

Laurence
  • 7,633
  • 21
  • 78
  • 129
  • Lin got the answer for you. I tend to use buttons without the submit to not worry about compatability issues of CSS formatting and submit the form using javascript ".submit();" – Ammar Dec 16 '13 at 16:59
  • Hi @AmmarAhmed .. Lin's answer is great .. I am a bit confused of what you have said .. Can I use normal button and do a POST all the data and bind them in the controller? I thought I have to use Submit buttons for the Form Post? – Laurence Dec 16 '13 at 18:55
  • I am a bit rusty and need to make an example. If I recall correctly, You don't need a submit button. You can use a div for example and have an onclick="submitForm()" java script to do it for you. It is over complicating this but I try to avoid buttons with submit on them because some browsers might render them differently. And make sure that your form is type ="POST" for this to work. – Ammar Dec 18 '13 at 20:04

1 Answers1

12

See below:

View:

<input type="submit" value="Save" name="buttonType" />

 <input type="submit" value="Publish" name="buttonType" />

Action

  public ActionResult MyAction(MyModel model, string buttonType)
    {
        if (buttonType == "Save")
        {
           // Do something for Save
        }
        if (buttonType == "Publish")
        {
        //Do something for Publish
        }
        return View(Model);
    }
Lin
  • 15,078
  • 4
  • 47
  • 49