title: Iterating ICollection from ViewModel within View
body: <p>I have two autogenerated database models (<strong>Product</strong> and <strong>ProductDetails</strong>) which I merged into a ViewModel so I can edit all data at once. </p>
  
  <p>What confuses me is the part where I am supposed to iterate through ICollection of Product_ProductCategoryAttributes (within ProductDetail model) inside a view to allow .NET automagically bind properties to the ViewModel. I have tried using <em>for</em> as well as <em>foreach</em> loop but without any success as controls are being created with wrong names (needed for auto binding). </p>
  
  <p><strong>Product model</strong></p>
  
  <pre><code>public partial class Product
  {
      public Product()
      {
          this.ProductDetail = new HashSet&lt;ProductDetail&gt;();
      }
  
      public int idProduct { get; set; }
      public int idProductCategory { get; set; }
      public string EAN { get; set; }
      public string UID { get; set; }
      public bool Active { get; set; }
  
      public virtual ProductCategory ProductCategory { get; set; }
      public virtual ICollection&lt;ProductDetail&gt; ProductDetail { get; set; }
  }
  </code></pre>
  
  <p><strong>ProductDetail model</strong></p>
  
  <pre><code>public partial class ProductDetail
  {
      public ProductDetail()
      {
          this.Product_ProductCategoryAttribute = new HashSet&lt;Product_ProductCategoryAttribute&gt;();
      }
  
      public int idProductDetail { get; set; }
      public int idProductCategory { get; set; }
      public int idMeta { get; set; }
      public int idProduct { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
  
      public virtual Meta Meta { get; set; }
      public virtual Product Product { get; set; }
      public virtual ICollection&lt;Product_ProductCategoryAttribute&gt; Product_ProductCategoryAttribute { get; set; }
      public virtual ProductCategory ProductCategory { get; set; }
  }
  </code></pre>
  
  <p><strong>ProductViewModel</strong> - One product can have many ProductDetails</p>
  
  <pre><code>public class ProductViewModel
  {
      public Product Product { get; set; }
      public List&lt;ProductDetail&gt; ProductDetails { get; set; }
  
  }
  </code></pre>
  
  <p><strong>View</strong> (some code is intentionally omitted)</p>
  
  <pre><code>@for (int i = 0; i &lt; Model.ProductDetails.Count(); i++)
  {
      @Html.TextAreaFor(model =&gt; model.ProductDetails[i].Description, new { @class = "form-control", @rows = "3" })
  
      @for (int j = 0; j &lt; Model.ProductDetails[i].Product_ProductCategoryAttribute.Count(); j++)
      {
         @Html.HiddenFor(model =&gt; model.ProductDetails[i].Product_ProductCategoryAttribute.ElementAt(j).idProductCategoryAttribute)
         @Html.TextBoxFor(model =&gt; model.ProductDetails[i].Product_ProductCategoryAttribute.ElementAt(j).Value, new { @class = "form-control" })
      }
   }
  </code></pre>
  
  <p>All controls outside the second <em>for loop</em> are being named properly eg. <strong>ProductDetails[0].Description</strong>, however controls generated within the second <em>for loop</em> get their name by the property value which in this case are <strong>Value</strong> and <strong>idProductCategoryAttribute</strong>. If I'm not wrong one solution would be converting ICollection to IList, but having model autogenerated I don't think it would be the best option.</p>