: MVC Html.CheckBox and form submit issue正确的提交形式与基于自动生成的控制
让我们看看下面的例子。查看:
<% using(Html.BeginForm("Retrieve", "Home")) %>
<% { %>
<%foreach (var app in newApps) { %>
<tr>
<td><%=Html.CheckBox(""+app.ApplicationId)%></td>
</tr>
<%} %>
<input type"submit"/>
<% } %>
控制器:
List<app>=newApps; //Database bind
for(int i=0; i<app.Count;i++)
{
var checkbox=Request.Form[""+app[i].ApplicationId];
if(checkbox!="false")// if not false then true,false is returned
}
建议的解决方案是关于的Request.Form的人工分析,似乎为我出MVC的概念。它使得这个控制器方法的单元测试成为问题。在这种情况下,我需要生成模拟的Request.Form对象,而不是作为输入参数传递的一些ViewModel。
问:是否有其他解决方案提交这样的表单,以便包含已提交控件集合的ViewModel对象作为输入参数传递给控制器方法?
例如:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Retrieve(AppList[] applist)
或
public ActionResult Retrieve(AppList<App> applist)
等
Andrey,我发布了另一个解决方案。尝试一下。 – 2009-06-17 20:04:41