2012-04-20 147 views
10

我跟着这个帖子上的说明: Asp.net mvc3 razor with multiple submit buttons 这里是我的模型:MVC4 - 一种形式2提交按钮

public class AdminModel 
{ 
    public string Command { get; set; } 
} 

我的控制器

[HttpPost] 
public ActionResult Admin(List<AdminModel> model) 
{ 
    string s = model.Command; 
} 

我查看

@using (Html.BeginForm("Admin", "Account")) 
{ 
    <input type="submit" name="Command" value="Deactivate"/> 
    <input type="submit" name="Command" value="Delete"/> 
} 

当我回发时,字符串“s”始终为空。

我也在这个论坛帖子中试过第二个答案(有146票的那个):How do you handle multiple submit buttons in ASP.NET MVC Framework?,那也是空的。我究竟做错了什么?

+0

http://stackoverflow.com/questions/2423041/using -two-submit-buttons-inside-single-form/2426152#2426152 这个怎么样? – takepara 2012-06-25 15:22:11

回答

24

需要通过按钮的名字从他们的服务器端的价值,

public ActionResult Admin(List<AdminModel> model,string Command) 
{ 
    string s = Command; 
} 
+0

whoa我以为我试过这个,每个论坛上的第二个论坛。那时我不知道我做错了什么。它现在有用,谢谢。 – 2012-04-20 16:58:40

+0

请注意,当使用'

0

从我在发布的代码中可以看到的内容中,您并没有将模型列表发送给您的控制器,只是一个模型实例。尝试将控制器修改本:

[HttpPost] 
public ActionResult Admin(AdminModel model) 
{ 
    string s = model.Command; 
} 
+0

我的观点是在一个AdminModel :)列表中。 – 2012-04-20 16:59:19