如何处理MVC视图上的两个提交按钮 [英] How to handle two submit buttons on MVC view

查看:138
本文介绍了如何处理MVC视图上的两个提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个MVC应用程序.这就像一个小型博客,用户可以在其中发布新闻.在这里,我想要两个提交按钮,并希望在控制器上以不同的方式处理它们.例如,一种用于发布,另一种用于保存,以便稍后发布.区别只是数据库中的布尔值,但我认为我不能在视图上有两个提交按钮或在控制器上有两个ActionResult Create.我知道我可以使用一个复选框来简化它,但是我不喜欢该复选框的可用性.处理此问题的最佳方法是什么?谢谢.

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.

推荐答案

请参见下文:

查看:

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

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

操作

  public ActionResult MyAction(MyModel model, string buttonType)
    {
        if (buttonType == "Save")
        {
           // Do something for Save
        }
        if (buttonType == "Publish")
        {
        //Do something for Publish
        }
        return View(Model);
    }

这篇关于如何处理MVC视图上的两个提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆