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...

Lawtonfogle
  • 974
  • 4
  • 17
  • 32
  • possible duplicate of [MVC which submit button has been pressed](http://stackoverflow.com/questions/1714028/mvc-which-submit-button-has-been-pressed) – Wahid Bitar Aug 19 '14 at 20:37
  • @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). – Lawtonfogle Aug 19 '14 at 20:45
  • But in your code the value of the first button isn't a number – Wahid Bitar Aug 19 '14 at 20:53
  • @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. – Lawtonfogle Aug 19 '14 at 20:55
  • And are you sure that the Action has parameter called `string submitButton` ?? – Wahid Bitar Aug 19 '14 at 21:04

1 Answers1

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
    }
}
Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106
  • You may found more useful information at this post http://weblogs.asp.net/dfindley/asp-net-mvc-multiple-buttons-in-the-same-form – Wahid Bitar Aug 19 '14 at 21:12
  • 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? – Lawtonfogle Aug 20 '14 at 13:08
  • I retested this morning and it is working. I'm now confused as to what was happening, but it works. – Lawtonfogle Aug 20 '14 at 13:10
  • Maybe you didn't rebuild your solution in the first time!. Anyway it's working now :) – Wahid Bitar Aug 21 '14 at 00:12
  • 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 browser – Robert Achmann Oct 21 '14 at 18:42