【发布时间】:2010-12-14 18:10:06
【问题描述】:
所以我有一个自定义 ViewModel (CompanySalaryDataViewModel),上面有一组其他 ViewModel (PersonalIncomeViewModel)。 最终,我想要一个 CompanySalaryDataViewModel 的 EditorTemplate,它以某种方式消耗 PersonalIncomeViewModel 的 EditorTemplate,当它被传递回我的 POST 控制器时,它提供了与我的 ViewModel 的双向绑定。强>
例如,假设它是这样的:
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>
这段代码的问题是:
- 每个“一月”文本框都具有相同的名称(
salary.JanIncome- 不包含任何将其与其他工资区分开来的内容)。每个月都有同样的问题。 - 可能是因为问题 #1,当我发帖时,这不会绑定回 ViewModel - 我的
AllSalaryData集合为空。 - 所有这些似乎都不是最佳实践,应该有更好的方法来做到这一点。
最终,我想我需要更好地理解将模板与集合一起使用,但另一部分是将t-alt 类正确应用到适当的表格行的“逻辑”。
注意 #1:我以这种方式为与这些文本框交互的一些 javascript 内容设置 HTML ID。
注意 #2:我并没有真正使用工资数据。
【问题讨论】:
-
好的,所以我找到了一个不理想但有效的函数解决方案。显然
foreach是问题所在,但for(var i=0; i < xxx.Count; i++)有效,因为索引号用于生成渲染控件的name。功能现在,但我想以正确的方式做到这一点。
标签: c# .net asp.net-mvc asp.net-mvc-2