2

我在更新对象的子集合(外键约束,带有子对象集合的 EF)时遇到问题,使用本指南解决了这个问题:http: //www.codetuning.net/blog/post/Binding-Model-Graphs-with- ASPNETMVC.aspx

在清理我的代码时,我将集合编辑移至局部视图。

@Html.Partial("_AttendeeInformationFields", Model.CaptureAttendeeInformationFields)

部分视图如下所示

@model ICollection<EventModel.Models.AttendeeInformationField>
<table id="CaptureAttendeeInformationFields">
    <tr>
        <th>@Html.GetDisplayName(model => model.FirstOrDefault().Name)</th>
        <th>@Html.GetDisplayName(model => model.FirstOrDefault().Required)</th>
        <th>@Html.GetDisplayName(model => model.FirstOrDefault().FieldType)</th>
        @*<th>@Html.GetDisplayName(model => model.FirstOrDefault().InputType)</th>*@
    </tr>
    @Html.EditorForModel()
</table>
@Html.LinkToAddNestedForm("Lägg till", "#CaptureAttendeeInformationFields", ".AttendeeInformationField", "CaptureAttendeeInformationFields", typeof(EventModel.Models.AttendeeInformationField))
@Html.ValidationMessageFor(model => model)

然后我有一个AttendeeInformationField 的EditorTemplate,看起来像这样

@model EventModel.Models.AttendeeInformationField
<tr class="AttendeeInformationField">
@using (Html.BeginCollectionItem("CaptureAttendeeInformationFields"))
{
    <td>@Html.TextBoxFor(model => model.Name) @Html.HiddenFor(model => model.MagnetEventId) @Html.HiddenFor(model => model.Id)</td>
    <td>@Html.CheckBoxFor(model => model.Required)</td>
    <td>@Html.DropDownListFor(model => model.FieldType, new SelectList(Enum.GetValues(typeof(EventModel.Models.FieldType)), Model.FieldType))</td>
    @*<td>@Html.TextBoxFor(model => model.InputType)</td>*@
}
</tr>

BeginCollectionItem 来自本指南: http: //ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/ 这有助于我做两件事 1 . 索引不再是一个从 0 开始的连续整数系列,我可以重新排序我的项目,以及添加/删除而不用担心破坏序列。2. 部分似乎失去了上下文,我的控件得到了像“[0].Required”这样的名称,它应该是“CaptureAttendeeInformationField[0].Required”。BeginCollectionItem 负责这个。

当前的问题是这些修复似乎不兼容。我想这可能与第一篇文章中的免责声明有关:

在这个实现中,我们假设索引是一个从 0 开始的整数

我希望有人能指出我正确的方向。在此解决方案中添加项目有效。

丑陋的解决方案 我当然希望这不是唯一的方法,但现在我已经解决了这样的问题:

foreach (var attendeeInformationField in viewModel.AttendeeInformationFields)
        {
            var attendeeInformationFieldId = attendeeInformationField.Id;
            var originalAttendeeInformationField = original.CaptureAttendeeInformationFields.FirstOrDefault(aif => aif.Id == attendeeInformationFieldId);
            if (originalAttendeeInformationField==null)
            {
                original.CaptureAttendeeInformationFields.Add(attendeeInformationField);
            }
            else
            {
                if (originalAttendeeInformationField != attendeeInformationField)
                {
                    originalAttendeeInformationField = attendeeInformationField;
                    originalAttendeeInformationField.FieldType = attendeeInformationField.FieldType;
                    //originalAttendeeInformationField.InputType = attendeeInformationField.InputType;
                    originalAttendeeInformationField.Name = attendeeInformationField.Name;
                    originalAttendeeInformationField.Required = attendeeInformationField.Required;
                }
            }
        }

我一点也不喜欢它,但它确实有效。必须有更好的方法来做到这一点。

4

0 回答 0