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");
}
-
Check this post weblogs.asp.net/dfindley/….Yevgeniy.Chernobrivets– Yevgeniy.Chernobrivets2014年06月25日 09:11:26 +00:00Commented Jun 25, 2014 at 9:11
-
stackoverflow.com/questions/23898553/…Amit– Amit2014年06月25日 09:21:47 +00:00Commented Jun 25, 2014 at 9:21
4 Answers 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.
1 Comment
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
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
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
});