0

In my MVC form starting with @Html.BeginForm I have put two submit buttons:

<div>
    <input id="submitButton" type="submit" value="Register" />
    <input type="hidden" name="btn" value="register" />
</div>

<div>
    <input id="cancelButton" type="submit" value="Cancel" />
    <input type="hidden" name="btn" value="cancel" />
</div>

But in my action method, the value that is coming in is still "register" from first button, even if I click the Cancel button.

How can I fix this?

  • Give each one the same `name` attribute and put it in your ActionResult method as a parameter. You can then check the value. – Drew Kennedy Apr 01 '16 at 15:40

1 Answers1

1

The hidden fields have nothing to do with the buttons, but their values are what you are receiving, regardless of which submit button you click.

Remove the hidden fields, and add the name="btn" attribute to each of the buttons. The value you receive in btn will be the value (and also the exact text) of the button that was pressed.

Example:

<div>
    <input id="submitButton" type="submit" name="btn" value="Register" />
</div>

<div>
    <input id="cancelButton" type="submit" name="btn" value="Cancel" />
</div>
Peter B
  • 22,460
  • 5
  • 32
  • 69