How to handle two submit buttons on MVC view

2019-04-02 12:01发布

问题:

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.

回答1:

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);
    }