그래서 다른 ViewModels (PersonalIncomeViewModel
) 컬렉션이있는 사용자 지정 ViewModel (CompanySalaryDataViewModel
)이 있습니다. 궁극적으로, CompanySalaryDataViewModel
에 대한 EditorTemplate을 가지고 싶습니다. PersonalIncomeViewModel
에 대해 EditorTemplate을 사용하는 것은 어떻게 든 내 POST 컨트롤러로 다시 전달 될 때 내 ViewModel에 대한 양방향 바인딩을 제공합니다.컬렉션에 대해이 EditorTemplate을 어떻게 만들 수 있습니까?
public class CompanySalaryDataViewModel
{
string CompanyName { get; set; }
IList<PersonalIncomeViewModel> AllSalaryData { get; set; }
}
public class PersonalIncomeViewModel
{
long UniqueID { get; set; }
string PersonName { get; set; }
int JanIncome { get; set; }
int FebIncome { get; set; }
int MarIncome { get; set; }
int AprIncome { get; set; }
int MayIncome { get; set; }
int JunIncome { get; set; }
int JulIncome { get; set; }
int AugIncome { get; set; }
int SepIncome { get; set; }
int OctIncome { get; set; }
int NovIncome { get; set; }
int DecIncome { get; set; }
}
나는 이것이는 다음과 같은 마크 업 비슷하지만 작동하는 방식으로 수행합니다 :이와
<%= Html.TextBoxFor(x => x.CompanyName) %>
<table cellspacing="0">
<thead>
<tr class="t-grid-header">
<th class="t-header">ID</th>
<th class="t-header">Person</th>
<th class="t-header">Jan</th>
<th class="t-header">Feb</th>
<th class="t-header">Mar</th>
<th class="t-header">Apr</th>
<th class="t-header">May</th>
<th class="t-header">Jun</th>
<th class="t-header">Jul</th>
<th class="t-header">Aug</th>
<th class="t-header">Sep</th>
<th class="t-header">Oct</th>
<th class="t-header">Nov</th>
<th class="t-header">Dec</th>
</tr>
</thead>
<%
var isAlt = false;
foreach (var salaryData in Model.AllSalaryData)
{
var salary = salaryData;
if (isAlt)
{%>
<tr class="t-alt">
<%
}
else
{%>
<tr>
<%
}
isAlt = !isAlt;
%>
<td><%=Html.TextBoxFor(x => salary.UniqueID)%></td>
<td><%=Html.TextBoxFor(x => salary.PersonName)%></td>
<td><%=Html.TextBoxFor(x => salary.JanIncome, new {id = salary.PersonName + "_1", style = "width: 45px;"})%></td>
<td><%=Html.TextBoxFor(x => salary.FebIncome, new {id = salary.PersonName + "_2", style = "width: 45px;"})%></td>
<td><%=Html.TextBoxFor(x => salary.MarIncome, new {id = salary.PersonName + "_3", style = "width: 45px;"})%></td>
<td><%=Html.TextBoxFor(x => salary.AprIncome, new {id = salary.PersonName + "_4", style = "width: 45px;"})%></td>
<td><%=Html.TextBoxFor(x => salary.MayIncome, new {id = salary.PersonName + "_5", style = "width: 45px;"})%></td>
<td><%=Html.TextBoxFor(x => salary.JunIncome, new {id = salary.PersonName + "_6", style = "width: 45px;"})%></td>
<td><%=Html.TextBoxFor(x => salary.JulIncome, new {id = salary.PersonName + "_7", style = "width: 45px;"})%></td>
<td><%=Html.TextBoxFor(x => salary.AugIncome, new {id = salary.PersonName + "_8", style = "width: 45px;"})%></td>
<td><%=Html.TextBoxFor(x => salary.SepIncome, new {id = salary.PersonName + "_9", style = "width: 45px;"})%></td>
<td><%=Html.TextBoxFor(x => salary.OctIncome, new {id = salary.PersonName + "_10", style = "width: 45px;"})%></td>
<td><%=Html.TextBoxFor(x => salary.NovIncome, new {id = salary.PersonName + "_11", style = "width: 45px;"})%></td>
<td><%=Html.TextBoxFor(x => salary.DecIncome, new {id = salary.PersonName + "_12", style = "width: 45px;"})%></td>
</tr>
<%
}%>
</table>
문제를 예를 들어
,의는이 같은 뭔가 가정 해 봅시다 코드는 다음과 같습니다.
- 각 "January"텍스트 상자는 동일한 이름 (
salary.JanIncome
- 다른 급여와 구별 할 수있는 항목이 없습니다). 매월 같은 문제가 있습니다. - 아마도 # 1 문제로 인해 게시 할 때 ViewModel에 다시 바인딩되지 않습니다. 내
AllSalaryData
컬렉션이 null입니다. - 이 모든 것은 매우 비범 한 것으로 보이며이를 수행하는 더 좋은 방법이 있어야합니다.
궁극적으로, 나는 더 나은 컬렉션으로 템플릿을 사용하여 이해할 필요가 있지만, 그것의 다른 부분이 제대로 적절한 테이블 행에 t-alt
클래스를 적용 할 수있는 "논리"입니다 같아요.
참고 # 1 :이 텍스트 상자와 상호 작용하는 일부 자바 스크립트 항목에 대해 HTML ID를 설정합니다.
참고 # 2 : 나는 급여 데이터로 이것을 실제로하고 있지 않습니다.
이상적이지 않은 기능 솔루션을 발견했지만 작동합니다. 분명히 'foreach'는 문제이지만, for (var i = 0; i
Jaxidian