1

I have a standard ASP MVC 4 form where the user is able to edit some details and click "Approve" or "Decline", which causes a post back.

When the controller receives the model in the post back, how do I know which button was clicked?

I'm looking for a standard way to do this so I don't mystify the next developer who works on the project.

Edit I'm using the stand post,

Kye
  • 5,919
  • 10
  • 49
  • 84
  • Can you add some detail about how you are posting the data back to the server? Is it just a standard HTML form, or do you use some ajax call when they click either button? – Jamiec Nov 30 '12 at 11:44
  • 1
    This similar question might help: [link](http://stackoverflow.com/q/442704/943359). I found this answer works well in MVC 3: [link](http://stackoverflow.com/a/443047/943359) – Tim S Nov 30 '12 at 12:05
  • Isn't the value of an input submitted? You could then use an if statement in the controller – Konstantin Nov 30 '12 at 12:23

4 Answers4

6

Give each button a name (same for each button) and value (unique for each button).

 <button type="submit" name="button" value="approve">Approve</button>
 <button type="submit" name="button" value="decline">Decline</button>

then add a string parameter to your post method called whatever name you assign to the buttons.

 [HttpPost]
    public ActionResult ProcessForm (Model model, string button)
    {
        //the parameter 'button' will contain the value of the button clicked - either 'approve' or 'decline'
    }
Forty-Two
  • 7,535
  • 2
  • 37
  • 54
3

A tricky question that has been wrestled with in every html format. It could come down to exactly how much data you need to post back... If you just need to identify the model and don't actually need to the form data then it can be easily achieved using two different forms that post to different controllers : very transparent and clear for the next developer:

<div class="container">

<form method="POST" action="/approve/123">
    <input type="submit">
</form>

<form method="POST" action="/decline/123">
    <input type="submit">
</form>

<div>

Alternatively, if you do need to the form data posted back I would attach an client side onclick event to each button that updates the form action accordingly. This however is not as clear for the next developer...

<div class="container">

<form method="POST" action="/">
    <input type="submit" value="Accept" id="btn-accept">
        <input type="submit" value="Decline" id="btn-decline">
</form>

</div>

$(function(){
$("#btn-accept").click(funcion(e)({
    e.preventDefault();
    $("form").attr("action", "/approve");
    $("form").submit();
}));
$("#btn-decline").click(funcion(e)({
    e.preventDefault();
    $("form").attr("action", "/decline");
    $("form").submit();
}));
});
Nick
  • 6,366
  • 5
  • 43
  • 62
2

what i think is the easiest is that
change the type of the input of the decline button from submit to button this will not post back to the controller then and you can then with few lines of code redirect the user to some other page

Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
1

You will want to do two separate "actions" for an "Approve" or a "Decline" right?

I would have an Approve Action in your controller and a Decline Action in your controller, so then they can Post to their own action, rather than trying to handle these two business processes in one controller action.

This will make it nice and clear what each action is responsible for.

SimonGates
  • 5,961
  • 4
  • 40
  • 52
  • I suspect the OP's next question will be "But how do I change the form action when they click the 2 buttons" – Jamiec Nov 30 '12 at 11:43
  • Yep. The user is entering details on an index page, but it would be great if I could post those details back to another event in the same controller depending on which button is pressed. – Kye Nov 30 '12 at 11:53