Note that there are some explanatory texts on larger screens.

plurals
  1. POModel binding nested collections in ASP.NET MVC
    primarykey
    data
    text
    <p>I'm using Steve Sanderson's <a href="http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/" rel="noreferrer">BeginCollectionItem helper</a> with ASP.NET MVC 2 to model bind a collection if items.</p> <p>That works fine, as long as the Model of the collection items does not contain another collection.</p> <p>I have a model like this:</p> <p>-Product<br> --Variants<br> ---IncludedAttributes</p> <p>Whenever I render and model bind the Variants collection, it works jusst fine. But with the IncludedAttributes collection, I cannot use the BeginCollectionItem helper because the id and names value won't honor the id and names value that was produced for it's parent Variant:</p> <pre><code>&lt;div class="variant"&gt; &lt;input type="hidden" value="bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126" autocomplete="off" name="Variants.index"&gt; &lt;input type="hidden" value="0" name="Variants[bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126].SlotAmount" id="Variants_bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126__SlotAmount"&gt; &lt;table class="included-attributes"&gt; &lt;input type="hidden" value="0" name="Variants.IncludedAttributes[c5989db5-b1e1-485b-b09d-a9e50dd1d2cb].Id" id="Variants_IncludedAttributes_c5989db5-b1e1-485b-b09d-a9e50dd1d2cb__Id" class="attribute-id"&gt; &lt;tr&gt; &lt;td&gt; &lt;input type="hidden" value="0" name="Variants.IncludedAttributes[c5989db5-b1e1-485b-b09d-a9e50dd1d2cb].Id" id="Variants_IncludedAttributes_c5989db5-b1e1-485b-b09d-a9e50dd1d2cb__Id" class="attribute-id"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; </code></pre> <p>If you look at the name of the first hidden field inside the table, it is Variants.IncludedAttributes - where it should have been Variants[bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126].IncludedAttributes[...]...</p> <p>That is because when I call BeginCollectionItem the second time (On the IncludedAttributes collection) there's given no information about the item index value of it's parent Variant.</p> <p>My code for rendering a Variant looks like this:</p> <pre><code>&lt;div class="product-variant round-content-box grid_6" data-id="&lt;%: Model.AttributeType.Id %&gt;"&gt; &lt;h2&gt;&lt;%: Model.AttributeType.AttributeTypeName %&gt;&lt;/h2&gt; &lt;div class="box-content"&gt; &lt;% using (Html.BeginCollectionItem("Variants")) { %&gt; &lt;div class="slot-amount"&gt; &lt;label class="inline" for="slotAmountSelectList"&gt;&lt;%: Text.amountOfThisVariant %&gt;:&lt;/label&gt; &lt;select id="slotAmountSelectList"&gt;&lt;option value="1"&gt;1&lt;/option&gt;&lt;option value="2"&gt;2&lt;/option&gt;&lt;/select&gt; &lt;/div&gt; &lt;div class="add-values"&gt; &lt;label class="inline" for="txtProductAttributeSearch"&gt;&lt;%: Text.addVariantItems %&gt;:&lt;/label&gt; &lt;input type="text" id="txtProductAttributeSearch" class="product-attribute-search" /&gt;&lt;span&gt;&lt;%: Text.or %&gt; &lt;a class="select-from-list-link" href="#select-from-list" data-id="&lt;%: Model.AttributeType.Id %&gt;"&gt;&lt;%: Text.selectFromList.ToLowerInvariant() %&gt;&lt;/a&gt;&lt;/span&gt; &lt;div class="clear"&gt;&lt;/div&gt; &lt;/div&gt; &lt;%: Html.HiddenFor(m=&gt;m.SlotAmount) %&gt; &lt;div class="included-attributes"&gt; &lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;&lt;%: Text.name %&gt;&lt;/th&gt; &lt;th style="width: 80px;"&gt;&lt;%: Text.price %&gt;&lt;/th&gt; &lt;th&gt;&lt;%: Text.shipping %&gt;&lt;/th&gt; &lt;th style="width: 90px;"&gt;&lt;%: Text.image %&gt;&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;% for (int i = 0; i &lt; Model.IncludedAttributes.Count; i++) { %&gt; &lt;tr&gt;&lt;%: Html.EditorFor(m =&gt; m.IncludedAttributes[i]) %&gt;&lt;/tr&gt; &lt;% } %&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; &lt;% } %&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>And the code for rendering an IncludedAttribute:</p> <pre><code>&lt;% using (Html.BeginCollectionItem("Variants.IncludedAttributes")) { %&gt; &lt;td&gt; &lt;%: Model.AttributeName %&gt; &lt;%: Html.HiddenFor(m =&gt; m.Id, new { @class = "attribute-id" })%&gt; &lt;%: Html.HiddenFor(m =&gt; m.ProductAttributeTypeId) %&gt; &lt;/td&gt; &lt;td&gt;&lt;%: Model.Price.ToCurrencyString() %&gt;&lt;/td&gt; &lt;td&gt;&lt;%: Html.DropDownListFor(m =&gt; m.RequiredShippingTypeId, AppData.GetShippingTypesSelectListItems(Model.RequiredShippingTypeId)) %&gt;&lt;/td&gt; &lt;td&gt;&lt;%: Model.ImageId %&gt;&lt;/td&gt; &lt;% } %&gt; </code></pre>
    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.
 

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