【问题标题】:Nested List of Lists with BeginCollectionItem带有 BeginCollectionItem 的嵌套列表列表
【发布时间】:2015-11-25 12:01:34
【问题描述】:

我正在使用 BeginCollectionItem 将项目添加到列表的内部列表。

我的问题与以下问题非常相似:Nested BeginCollectionItem,但是包含列表的对象本身也是一个列表(因此在引用的示例中,tt 是一个列表,而不仅仅是对象的一个​​实例)。

我的模特是:

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 对象未填充。有什么建议吗?

【问题讨论】:

  • ViewData.TemplateInfo.HtmlFieldPrefix; 不会返回正确的前缀(由外部 BeginCollectionItem() 生成的前缀)。你看问题中的article了吗?
  • 我没看过这篇文章!刚刚实现了对 BeginCollectionItem 方法的更改,并且一切正常 - 谢谢!
  • 很抱歉唤醒了一个老问题,但我遇到了与开瓶器相同的问题,但 user3559349 提供的链接不起作用...请帮助!
  • @patsy2k - 我也是。你找到答案了吗?

标签: c# asp.net-mvc begincollectionitem


【解决方案1】:

我设法使其工作如下。感谢 user3559349 对 ViewData.TemplateInfo.HtmlFieldPrefix 的领导

型号:

public class MainDto
{
    public List<ItemDto> Items { get; set; }
}
public class ItemDto
{
    public string Label { get; set; }
    public List<SubItemDto> SubItems { get; set; }
}
public class SubItemDto
{
    public int Value { get; set; }
}

主视图:

<table>
@foreach (var item in Model.Items)
{
    @Html.Partial("_ItemEditor", item)
}
</table>

项目编辑器模板:

@model Test.ItemDto
<tr>
    @using (Html.BeginCollectionItem("Items"))
    {
        <td>@Html.TextBoxFor(x => x.Label)</td>
        foreach (var subItem in Model.SubItems)
        {
            @Html.Partial("_SubItemEditor", subItem)
        }
    }
</tr>

子项目编辑器模板:

@model Test.SubItemDto
@using (Html.BeginCollectionItem(ViewData.TemplateInfo.HtmlFieldPrefix + ".SubItems"))
{
    <td>@Html.TextBoxFor(x => x.Value)</td>
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 2012-10-25
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    相关资源
    最近更新 更多