Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

asp.net mvc - How to handle two submit buttons on MVC view

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...