BeginCollectionItem を使用して、リストの内部リストに項目を追加しています。
私の質問は次のように非常に似ています:ネストされた BeginCollectionItem、ただし、リストを含むオブジェクトはリスト自体でもあります(したがって、参照されている例では、tt はオブジェクトの 1 つのインスタンスではなくリストです)。
私のモデルは次のとおりです。
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; }
}
したがって、私の見解では、完全な週のタイムシートを表すフォームがあるため、日のリスト (公開リスト日) と、それに対して予約する時間のリスト (公開リスト時間) があります。
私のビューは次のようになります: 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 (エディター テンプレート内)
@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 (エディター テンプレート)
@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 オブジェクトは読み込まれません。助言がありますか?