MVC 提交按钮已被按下 [英] MVC which submit button has been pressed

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

问题描述

我的 MVC 表单上有两个按钮:

I have two buttons on my MVC form:

<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />

从我的控制器操作中我怎么知道哪个按钮被按下了?

From my Controller action how do I know which one have been pressed?

推荐答案

两个提交按钮的名称相同

Name both your submit buttons the same

<input name="submit" type="submit" id="submit" value="Save" />
<input name="submit" type="submit" id="process" value="Process" />

然后在您的控制器中获取提交的值.只有被点击的按钮才会传递它的值.

Then in your controller get the value of submit. Only the button clicked will pass its value.

public ActionResult Index(string submit)
{
    Response.Write(submit);
    return View();
}

您当然可以评估该值以使用 switch 块执行不同的操作.

You can of course assess that value to perform different operations with a switch block.

public ActionResult Index(string submit)
{
    switch (submit)
    {
        case "Save":
            // Do something
            break;
        case "Process":
            // Do something
            break;
        default:
            throw new Exception();
            break;
    }

    return View();
}

这篇关于MVC 提交按钮已被按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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