MVC which submit button has been pressed

2019-01-03 09:41发布

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?

10条回答
虎瘦雄心在
2楼-- · 2019-01-03 09:59

you can identify your button from there name tag like below, You need to check like this in you controller

if (Request.Form["submit"] != null)
{
//Write your code here
}
else if (Request.Form["process"] != null)
{
//Write your code here
}
查看更多
干净又极端
3楼-- · 2019-01-03 10:00
// Buttons
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />

// Controller
[HttpPost]
public ActionResult index(FormCollection collection)
{
    string submitType = "unknown";

    if(collection["submit"] != null)
    {
        submitType = "submit";
    }
    else if (collection["process"] != null)
    {
        submitType = "process";
    }

} // End of the index method
查看更多
Deceive 欺骗
4楼-- · 2019-01-03 10:02

Can you not find out using Request.Form Collection? If process is clicked the request.form["process"] will not be empty

查看更多
成全新的幸福
5楼-- · 2019-01-03 10:06

This post is not going to answer to Coppermill, because he have been answered long time ago. My post will be helpful for who will seeking for solution like this. First of all , I have to say " WDuffy's solution is totally correct" and it works fine, but my solution (not actually mine) will be used in other elements and it makes the presentation layer more independent from controller (because your controller depend on "value" which is used for showing label of the button, this feature is important for other languages.).
Here is my solution, give them different names , Like:

 </p>
        <input type="submit" name="buttonSave" value="Save"/>
        <input type="submit" name="buttonProcess" value="Process"/>
        <input type="submit" name="buttonCancel" value="Process"/>
    </p>

`
And you must specify the names of buttons as arguments in the action like below:

    public ActionResult Register(string buttonSave, string buttonProcess, string buttonCancel){if (buttonSave!= null)
{//save is pressed
}
if (buttonProcess!= null)
{//Process is pressed
}
if (buttonCancel!= null)
{//Cancel is pressed
}


when user submits the page using one of the buttons, only one of the arguments will have value. I guess this will be helpful for others.
Update
This answer is quite old and I actually reconsider my opinion . maybe above solution is good for situation which passing parameter to model's properties. don't bother yourselves and take best solution for your project.

查看更多
三岁会撩人
6楼-- · 2019-01-03 10:08

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

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();
}
查看更多
在下西门庆
7楼-- · 2019-01-03 10:10

this is a better answer, so we can have both text and value for a button:

http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx

</p>
<button name="button" value="register">Register</button>
<button name="button" value="cancel">Cancel</button>
</p>

and the controller:

public ActionResult Register(string button, string userName, string email, string password, string confirmPassword)
{
if (button == "cancel")
    return RedirectToAction("Index", "Home");
...

in short its a SUBMIT button but you choose the name using the name attribute, its even more powerful because your not obligated to the name submit or button in the controller method parameters, you can call it as you like...

查看更多
登录 后发表回答