How to determine which submit button is clicked in asp.net (mvc)

I have read a lot of answers that use submit type input value, but my collection of input buttons should have the same text. Others use Javascript and I try to avoid that too.

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

      

Doesn't work because they all have to be named "Click this".

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

      

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

Is there a way to make it <button>

send its value or change the text <input type="submit">

so they all say the same thing on the page, having different meanings? Or perhaps even hiding the numeric value in the value attribute of the input element and then just removing the "Push this" before using the value?

Perhaps using <input type="image" value="12" />

with an image that says "Click this"?

Edit: Tried <input type="image">

it and it doesn't work. It will submit the form but does not use the name attribute to navigate to the correct action on the controller.

Edit2: I also have to add, the number of submit buttons is dynamic and so I cannot give them all different names and then see which parameter in the controller has the value passed to it. If there is some way to do this for an unknown number of buttons ...

+3


source to share


1 answer


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 action

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

    if (button == "13"){
        //Do that
    }
}

      

+10


source







All Articles