10

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?

Community
  • 1
  • 1
Vijay V
  • 389
  • 5
  • 11
  • 23
  • http://stackoverflow.com/questions/2423041/using-two-submit-buttons-inside-single-form/2426152#2426152 How about this? – takepara Jun 25 '12 at 15:22

2 Answers2

24

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;
}
Jayantha Lal Sirisena
  • 20,611
  • 10
  • 65
  • 90
  • whoa 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 Apr 20 '12 at 16:58
  • Note that when using ` – Cocowalla Feb 03 '13 at 16:12
0

From what I can see in the posted code, you aren't sending a list of models to your controller, just a single model instance. Try modifying the controller to this:

[HttpPost]
public ActionResult Admin(AdminModel model)
{
   string s = model.Command;
}
Derek Risling
  • 354
  • 1
  • 5