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");
}
2

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the simplest, clearest version of solving this issue that I've seen.
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");
    }

Comments

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)

Comments

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
});

2 Comments

he wants only one action method and he wants to identify which button is pressed I guess you are not justifying his question
@Neel i didn't suggested two have to action method, the property in model will just have the action performed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.