michael
michael

Reputation: 585

how to have two buttons in my view to same action method asp.net mvc

I have two submit buttons which call the same action method. How can I tell which of these buttons was clicked in the formcollection of the action method (without setting the value property of the buttons)?

HTML code for buttons:

<input type="submit" name="button" />
<input type="submit" name="button"  />

Action method as:

public ActionResult submitted(FormCollection form)
{
}

i know how to do if we have a value property, but I just want to try like this without value property. How can this be done?

thanks,
michaeld

Upvotes: 0

Views: 169

Answers (1)

Matthew Abbott
Matthew Abbott

Reputation: 61589

The best thing to do, is intercept the click action to set a hidden form variable before the form is submitted, e.g.:

<script language="text/javascript">
    $("form input[submit]").click(function() {
        $("#buttonSelected").val("some unique value here");
    });
</script>

Where you might have a hidden input:

<input type="hidden" id="buttonSelected" name="buttonSelected" />

That way, you can then check the specific "buttonSelected" form value to figure out which button was pressed.

Upvotes: 1

Related Questions