Note that there are some explanatory texts on larger screens.

plurals
  1. POEditing a collection in a partial view
    primarykey
    data
    text
    <p>I was having problems updating child collections of my object ( <a href="https://stackoverflow.com/questions/10170101/foreign-key-constraint-ef-with-collection-of-childobjects/">Foreign key constraint, EF with collection of childobjects</a> ) which was solved using this guide: <a href="http://www.codetuning.net/blog/post/Binding-Model-Graphs-with-ASPNETMVC.aspx" rel="nofollow noreferrer">http://www.codetuning.net/blog/post/Binding-Model-Graphs-with-ASPNETMVC.aspx</a></p> <p>When cleaning up my code, I moved the collection editing to a partial view.</p> <pre><code>@Html.Partial("_AttendeeInformationFields", Model.CaptureAttendeeInformationFields) </code></pre> <p>The partial view looks like this</p> <pre><code>@model ICollection&lt;EventModel.Models.AttendeeInformationField&gt; &lt;table id="CaptureAttendeeInformationFields"&gt; &lt;tr&gt; &lt;th&gt;@Html.GetDisplayName(model =&gt; model.FirstOrDefault().Name)&lt;/th&gt; &lt;th&gt;@Html.GetDisplayName(model =&gt; model.FirstOrDefault().Required)&lt;/th&gt; &lt;th&gt;@Html.GetDisplayName(model =&gt; model.FirstOrDefault().FieldType)&lt;/th&gt; @*&lt;th&gt;@Html.GetDisplayName(model =&gt; model.FirstOrDefault().InputType)&lt;/th&gt;*@ &lt;/tr&gt; @Html.EditorForModel() &lt;/table&gt; @Html.LinkToAddNestedForm("Lägg till", "#CaptureAttendeeInformationFields", ".AttendeeInformationField", "CaptureAttendeeInformationFields", typeof(EventModel.Models.AttendeeInformationField)) @Html.ValidationMessageFor(model =&gt; model) </code></pre> <p>Then I have a EditorTemplate for AttendeeInformationField that looks like this</p> <pre><code>@model EventModel.Models.AttendeeInformationField &lt;tr class="AttendeeInformationField"&gt; @using (Html.BeginCollectionItem("CaptureAttendeeInformationFields")) { &lt;td&gt;@Html.TextBoxFor(model =&gt; model.Name) @Html.HiddenFor(model =&gt; model.MagnetEventId) @Html.HiddenFor(model =&gt; model.Id)&lt;/td&gt; &lt;td&gt;@Html.CheckBoxFor(model =&gt; model.Required)&lt;/td&gt; &lt;td&gt;@Html.DropDownListFor(model =&gt; model.FieldType, new SelectList(Enum.GetValues(typeof(EventModel.Models.FieldType)), Model.FieldType))&lt;/td&gt; @*&lt;td&gt;@Html.TextBoxFor(model =&gt; model.InputType)&lt;/td&gt;*@ } &lt;/tr&gt; </code></pre> <p>The BeginCollectionItem is from this guide: <a href="http://ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/" rel="nofollow noreferrer">http://ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/</a> This helps me with two things 1. The index is no longer a sequential 0-based integer series, and I can reorder my items, as well as add/delete without worrying about breaking the sequence. 2. The partial seems to loose the context, and my controls get names like "[0].Required" where it should be "CaptureAttendeeInformationField[0].Required". The BeginCollectionItem takes care of this.</p> <p>The current problem is that these fixes doesn't seem to be compatible. I suppose it might have something to do with this disclaimer in the first article:</p> <blockquote> <p>within this implementation we assume that the index is an integer starting at 0</p> </blockquote> <p>I'm hoping that someone can point me in the right direction. Adding items in this solution works.</p> <p><strong>Ugly solution</strong> I sure hope this is not the only way to do this, but for now I've solved the problem like this:</p> <pre><code>foreach (var attendeeInformationField in viewModel.AttendeeInformationFields) { var attendeeInformationFieldId = attendeeInformationField.Id; var originalAttendeeInformationField = original.CaptureAttendeeInformationFields.FirstOrDefault(aif =&gt; 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; } } } </code></pre> <p>I don't like it at all, but it works. There must be a better way of doing this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload