자가 생성 대조군이있는 양식의 적절한 제출
-
05-07-2019 - |
문제
기반 :MVC HTML.CHECKBOX 및 양식 제출 문제
다음 예를 고려해 봅시다. 보다:
<% 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
}
제안 된 솔루션은 MVC 개념에서 나에게 보이는 Request.form의 수동 구문 분석에 관한 것입니다. 이 컨트롤러 방법을 단위 테스트하는 동안 문제가 발생합니다. 이 경우 Mock request.form 객체를 입력 매개 변수로 전달한 뷰 모델 대신에 객체를 생성해야합니다.
Q : 이와 같은 양식을 제출하는 다른 솔루션이 있으므로 제출 된 컨트롤 모음이 포함 된 뷰 모델 객체가 입력 매개 변수로 전달 되었습니까?
예를 들어:
[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" />
<% } %>
이것을 읽으십시오 : Scott Hanselman의 Computerzen.com -ASP.NET 와이어 형식 어레이, 목록, 컬렉션, 사전에 모델 바인딩
업데이트 :
ApplicationID가 DB의 핵심 인 경우 사용하는 것이 좋습니다. 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