asp.net-mvc MVC4 - 一个表单 2 提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10250167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC4 - One form 2 submit buttons
提问by Vijay V
I followed the instructions on this post: Asp.net mvc3 razor with multiple submit buttonsand here is my model:
我按照这篇文章的说明操作: Asp.net mvc3 razor with multiple submit buttons,这是我的模型:
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.
当我回发时,字符串“s”始终为空。
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?
我还尝试了此论坛帖子中的第二个答案(获得 146 票的答案):How do you handle multiple submit buttons in ASP.NET MVC Framework? 那也是空的。我究竟做错了什么?
回答by Jayantha Lal Sirisena
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;
}
回答by Derek Risling
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;
}

