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?

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

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

Need Your Help

mvc5 attribute routing within area can't find view

c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-areas attributerouting

When I'm inside Admin area and map my routes using attribute routing it cannot find view because it doesn't look inside actual area view folders but instead only global view folders.

Custom Home Icon in Action Bar Sherlock

android drawable android-actionbar actionbarsherlock

I am trying to set custom icon for home icon using ActionBarSherlock library. I have tried to set custom layout using abHomeLayout attribute in my custom theme. But it didn't work for me. Only way,...