Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 - One form 2 submit buttons

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?

like image 553
Vijay V Avatar asked Apr 20 '12 16:04

Vijay V


1 Answers

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;
}
like image 171
Jayantha Lal Sirisena Avatar answered Nov 03 '22 07:11

Jayantha Lal Sirisena