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

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

问题描述

我有我的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" />

从我的控制器动作我怎么知道哪一个已经pssed $ P $?

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其中提交按钮已被pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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