【发布时间】:2013-08-16 08:35:04
【问题描述】:
我的控制器上有一个动作,它采用两个参数,在发布表单时应该捕获这些参数:
[HttpPost]
public ActionResult Index(MyModel model, FormAction action)
想法是模型数据应该在MyModel中捕获,用户按下的按钮应该在FormAction中捕获:
public class MyModel
{
public string MyValue { get; set; }
}
public class FormAction
{
public string Command { get; set; }
}
这是我的看法:
@model TestApp.Models.MyModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@using (Html.BeginForm("Index", "Home"))
{
@Html.TextBoxFor(x => x.MyValue)
<input type="submit" value="OK" name="command" />
<input type="submit" value="Cancel" name="command" />
}
</div>
</body>
</html>
如果我向名为“command”的操作添加另一个字符串参数,则按钮的值会通过,但它不会绑定到 FormAction 参数上的 Command 属性 - 该参数始终为空。
如果我将Command 属性添加到MyModel,那么按钮值就会通过。
MVC 模型绑定中是否存在阻止在一个操作方法中绑定多个复杂模型的内容?
【问题讨论】: