MVC4 - One form 2 submit buttons
21,904
you need to take the value from their server side by the name of the button,
public ActionResult Admin(List<AdminModel> model,string Command)
{
string s = Command;
}
Author by
Vijay V
Updated on June 26, 2020Comments
-
Vijay V about 4 years
I followed the instructions on this post: Asp.net mvc3 razor with multiple submit buttons and here is my model:
public class AdminModel { public string Command { get; set; } }
My Controller
[HttpPost] public ActionResult Admin(List<AdminModel> model) { string s = model.Command; }
My View
@using (Html.BeginForm("Admin", "Account")) { <input type="submit" name="Command" value="Deactivate"/> <input type="submit" name="Command" value="Delete"/> }
When I post back, string "s" is always null.
I also tried the second answer (the one with 146 votes) in this forum post : How do you handle multiple submit buttons in ASP.NET MVC Framework? and thats also null. What am I doing wrong?
-
Vijay V about 12 yearswhoa I thought I tried this, per the second forum on my post. I dont know what I did wrong that time. it works now thanks.
-
Vijay V about 12 yearsMy view was taking in a list of AdminModel :).
-
Cocowalla over 11 yearsNote that when using
<button>
tags and not<input>
tags, pressing Enter in one of the form fields, theCommand
argument will not get set and will therefore benull
. You can handle that by using Javascript to capture key press events, or by simply by setting a default value in the controller action:public ActionResult Admin(List<AdminModel> model, string Command = "Deactivate")