我正在使用BeginCollectionItem将项目添加到列表的内部列表中。
我的问题非常类似于以下内容:Nested BeginCollectionItem,但是我的包含列表的对象本身也是一个列表(因此在引用的示例中,tt是一个List而不仅仅是一个对象的实例)。
我的模特是:
public class WeeklyTimeSheetViewModel
{
public Guid WeeklyId { get; set; }
public DateTime WeekCommencing { get; set; }
public int WeekNumber { get; set; }
public Employee Employee { get; set; }
public List<TimeSheetDayViewModel> Days{ get; set; }
public double HourlyRate { get; set; }
public double OvernightRate { get; set; }
}
public class TimeSheetDayViewModel
{
public Guid DayId { get; set; }
public DateTime Date { get; set; }
public List<TimeBookingViewModel> Hours { get; set; }
public bool Overnight { get; set; }
}
public class TimeBookingViewModel
{
public Guid BookingId { get; set; }
public string TimeRef { get; set; }
public string TimeDescription { get; set; }
public double NormalHours { get; set; }
public double OvertimeHoursR1 { get; set; }
public double OvertimeHoursR2 { get; set; }
}
因此,在我看来,我有一个表格,代表一整周的时间表,所以一个天数列表(公共列表天数),然后每天都有一个小时列表(公共列表小时)。< / p>
我的观点如下: WeeklyTimeSheet.cshtml
@model Models.WeeklyTimeSheetViewModel
<div style="margin: 5px;">
<h1>Time Sheet</h1>
@using (Html.BeginForm(FormMethod.Post))
{
...
@Html.EditorFor(model => model.Days)
...
}
</div>
TimeSheetDayViewModel.cshtml(在editortemplates中)
@using (Html.BeginCollectionItem("Days"))
{
<table class="formtable">
@Html.HiddenFor(model => model.DayId)
@Html.HiddenFor(model => model.Date)
<thead>
<tr>
<th class="formtableheader" colspan="6">@Model.Date.DayOfWeek @Model.Date.ToLongDateString()</th>
</tr>
<tr>
<td class="formtabletools formtableleft">Overnight: @Html.CheckBoxFor(model => model.Overnight)</td>
<td class="formtabletools formtableright" colspan="5">
<a href="javascript:void(0)" onclick="AddTimeBooking('@Model.Date.ToShortDateString()');">add</a>
</td>
</tr>
<tr class="formtablecolumns">
<th>Ref</th>
<th>Description</th>
<th>Hours</th>
<th>Overtime x1.5</th>
<th>Overtime x2.0</th>
<th></th>
</tr>
</thead>
<tbody id="@Model.Date.ToShortDateString()">
@Html.EditorFor(model => model.Hours)
</tbody>
</table>
}
TimeBookingViewModel.cshtml(editortemplates)
@model Models.TimeBookingViewModel
@{
var methodPrefix = ViewData.TemplateInfo.HtmlFieldPrefix;
}
@using (Html.BeginCollectionItem(methodPrefix + ".Hours"))
{
@Html.HiddenFor(model => model.BookingId)
<tr>
<td>@Html.TextBoxFor(model => model.TimeRef, new {@class = "short"})</td>
<td>@Html.TextBoxFor(model => model.TimeDescription, new {@class = "longer"})</td>
<td>@Html.TextBoxFor(model => model.NormalHours, new {@class = "tiny"})</td>
<td>@Html.TextBoxFor(model => model.OvertimeHoursR1, new {@class = "tiny"})</td>
<td>@Html.TextBoxFor(model => model.OvertimeHoursR2, new {@class = "tiny"})</td>
<td><a href="javascript:void(0)" onclick="DeleteTimeBooking(event)">delete</a></td>
</tr>
}
在回发到服务器的帖子上没有填充TimeBookingViewModel对象。有什么建议吗?