适当的提交形式与自动生成的控制
-
05-07-2019 - |
题
让我们考虑下述例子。观点:
<% 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
}
提出的解决方案是有关的手册分析的请求。形式,似乎对我的视的概念。它使得该问题,同时单元的测试这个控制器的方法。在这种情况下,我需要生成模拟的请求。形成对象,而不是一些模型作为传递的输入参数。
问:是有一些其他的解决方案的提交形式就像这样,因此模型对象,包含收集提交的控制,作为传递的输入参数的控制器的方法?
例如:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Retrieve(AppList[] applist)
或
public ActionResult Retrieve(AppList<App> applist)
等等
解决方案
控制器:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Retrieve(AppList[] applist)
观点:
<% using(Html.BeginForm("Retrieve", "Home")) %> { %>
<%foreach (var app in newApps) { %>
<tr>
<td><%=Html.CheckBox(String.Format("appList[{0}].AProperty", app.ApplicationId) %></td>
</tr>
<% } %>
<input type"submit" />
<% } %>
读取这样的: 斯科特Hanselman的ComputerZen.com -ASP.NET 丝格式,用于模型的结合阵列,列出、收藏、字典
更新:
如果Editor是一个关键从数据库最好是使用 AppList<App>
作为行动的参数。然后您的形式将是在寻找为:
<% using(Html.BeginForm("Retrieve", "Home")) %> { %>
<% var counter = 0; %>
<% foreach (var app in newApps) { %>
<tr>
<td><%=Html.CheckBox(String.Format("appList[{0}].Key", counter), app.ApplicationId) %></td>
<!-- ... -->
<td><%=Html.Input(String.Format("appList[{0}].Value.SomeProperty1", counter), app.SomeProperty1) %></td>
<td><%=Html.Input(String.Format("appList[{0}].Value.SomePropertyN", counter), app.SomePropertyN) %></td>
<% counter = counter + 1; %>
</tr>
<% } %>
<input type"submit" />
<% } %>
不隶属于 StackOverflow