1

I have a single MVC form with two buttons, the page basically loads a record and the user has a approve or reject button

 <button id="btnAcceptAll" class="btn btn-lg btn-success">Accept</button>
 <button id="btnRejectAll" class="btn btn-lg btn-danger">Reject</button>

Id like to keep it as a single form because I have a lot of hidden value I use as well.

Basically in the Form Post I wanna be able to distinguish which button was pressed.

My Form Post at the moment..

[ActionName("Index")]
[HttpPost]
public ActionResult IndexPost(QcMatchViewModel model)
{
    //Save Record and Redirect
    return RedirectToAction("Index");
}
tereško
  • 58,060
  • 25
  • 98
  • 150
StevieB
  • 6,263
  • 38
  • 108
  • 193

4 Answers4

4

You can add name attribute to both buttons with the same value and then add value attribute.

<button id="btnAcceptAll" name="button" value="accept" class="btn btn-lg btn-success">Accept</button>
<button id="btnRejectAll" name="button" value="reject" class="btn btn-lg btn-danger">Reject</button>

After that in your action method you need to add additional button parameter which will be filled with value attribute of button pressed.

[ActionName("Index")]
[HttpPost]
public ActionResult IndexPost(string button, QcMatchViewModel model)
{
    //Save Record and Redirect
    return RedirectToAction("Index");
}

Or you can put additional Button property to your view model.

Check this post for some additional features.

Yevgeniy.Chernobrivets
  • 3,194
  • 2
  • 12
  • 14
1

Try This

on View :

<input type="submit" id="btnAcceptAll" name="btnAcceptAll" value"Accept" class="btn btn-lg btn-success"/ >
<input type="submit" id="btnRejectAll" name="btnRejectAll" value"Reject" class="btn btn-lg btn-success"/ >

On Controller:

[ActionName("Index")]
    [HttpPost]
    public ActionResult IndexPost(FormCollection form, QcMatchViewModel model)
    {
      if(form["btnAcceptAll"]!=null)
       {
        //Accept Button Code
       }
       if(form["btnRejectAll"]!=null)
       {
        //Reject Button Code
       }
        //Save Record and Redirect
        return RedirectToAction("Index");
    }
Amit
  • 823
  • 7
  • 13
0

There are 2 possible wayouts :

1> Set one property of that model to some value on either button click. i.e. let's say have a property named "accept" and on click of btnAcceptAll, set its value to true using javascript/jquery (Take accept as a hidden field) and on action method you can get that value.

2> Have 2 different action methods namely acceptChanges and RejectChanges and call them based on which button is clicked (Again using Jquery to set different form action on button click)

Manthan
  • 79
  • 1
  • 2
0

On approach that is coming to my mind You can have a field in your model, whose value can be filled by a hidden field So you will be getting this value on post.

Now to fill value in this hidden field, you can intercept the button click event.

$(".btn").click(function(){
//fill hidden field value here
});