How to detect which submit button was clicked in asp.net (mvc)
Asked Answered
K

1

7

I've read plenty of answers that use the value of submit type input, but my collection of input buttons need to all have the same text. Others use Javascript, and I'm trying to avoid that as well.

<input type="submit" value="Press This" name="submitButton" />

Doesn't work because they all need to be named 'Press This'.

<button type="submit" value="12" name="submitButton">Press This</button>

Doesn't work because it doesn't post the value.

Is there some way to make the <button> submit it's value or to change the text of the <input type="submit"> so they all say the same on the page while having different values? Or possibly even hiding the numeric value in the value attribute of the input element and then just removing the "Press This" before using the value?

Perhaps using <input type="image" value="12" /> with a image that says "Press This"?

Edit: Tried the <input type="image"> and it doesn't work. It'll submit the form, but it doesn't use the name attribute to go to the correct action on the controller.

Edit2: I should also add, the number of submit buttons is dynamic and thus I cannot give them all different names and then see which parameter in the controller had a value passed to it. Unless there is some way to do this for a unknown number of buttons...

Knisley answered 19/8, 2014 at 20:32 Comment(5)
possible duplicate of MVC which submit button has been pressedEndometriosis
@WahidBitar It is not a duplicate because that solution requires the value submitted to equal the value displayed while I need them to be different (the value submitted is a number, that value displayed is "Press This" on all the submit buttons).Knisley
But in your code the value of the first button isn't a numberEndometriosis
@WahidBitar Because if I made it the number I need it to be, that would be how it would display on the page. For the first way to work, I would need the value to be "Press This" and "12" at the same time, thus the problem.Knisley
And are you sure that the Action has parameter called string submitButton ??Endometriosis
E
15

your buttons should look like this:

<button name="button" value="12">Press This</button>
<button name="button" value="13">Press That</button>

then just get them in the action

public ActionResult MyAction(string button)
{
    if (button == "12"){
        //Do this
    }

    if (button == "13"){
        //Do that
    }
}
Endometriosis answered 19/8, 2014 at 21:9 Comment(5)
You may found more useful information at this post weblogs.asp.net/dfindley/…Endometriosis
I've tried this, and I only receive back a 0 regardless of what is pressed. The same name on the input type=submit does post back the value. Are there any special concerns with what the button must be named?Knisley
I retested this morning and it is working. I'm now confused as to what was happening, but it works.Knisley
Maybe you didn't rebuild your solution in the first time!. Anyway it's working now :)Endometriosis
If using VS to debug, don't forget to kill the IIS Express tool in the bottom right Notification Icons Tray, or new JS etc, may not get to your test browserMaxa

© 2022 - 2024 — McMap. All rights reserved.