title: How to Edit Enumerable or List View in MVC Net Core
body: <p>How do I create a Edit View for a List?</p>
  
  <p>The following is a sample view and model for a single item, which works correctly.
  Next question is, I have a list of items I want to edit. How would I conduct this?</p>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>public partial class ProductViewModel
  {
      public int ProductId { get; set; }
      public string ProductName { get; set; }
      public string ProductDescription { get; set; }
      public string ImageLocation { get; set; }
  }
  </code></pre>
  
  <p><strong>Successful View:</strong></p>
  
  <pre><code>@model ProductViewModel
  
  @{
      ViewData["Title"] = "Edit";
  }
  
  &lt;h2&gt;Edit&lt;/h2&gt;
  
  &lt;h4&gt;Product&lt;/h4&gt;
  &lt;hr /&gt;
  &lt;div class="row"&gt;
      &lt;div class="col-md-4"&gt;
          &lt;form asp-action="Edit"&gt;
              &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
              &lt;input type="hidden" asp-for="ProductId" /&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="ProductName" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="ProductName" class="form-control" /&gt;
                  &lt;span asp-validation-for="ProductName" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="ProductDescription" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="ProductDescription" class="form-control" /&gt;
                  &lt;span asp-validation-for="ProductDescription" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;input type="submit" value="Save" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Now, I have a model with a list of items, how do I access them in the view? I want to edit all the items in my shopping cart, more specifically the quantity. Trying to call the delegate linq queries correctly.</p>
  
  <p><strong>Error:</strong> </p>
  
  <blockquote>
    <p>The name 'modelItem' does not exist in the current context.</p>
  </blockquote>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>using System.Collections.Generic;   
  
  public class ShoppingCart : List&lt;CartLine&gt;
  {
      public ShoppingCart() { }
      public ShoppingCart(IEnumerable&lt;CartLine&gt; collection) : base(collection) { }
      public ShoppingCart(int capacity) : base(capacity) { }
  }    
  
  public class ShoppingCartViewModel
  {
      public ShoppingCart ShoppingCart { get; set; }
      public string ReturnUrl { get; set; }
  }
  
  public class CartLine
  {
      public int CartLineId { get; set; }
      public Product Product { get; set; }
      public int Quantity { get; set; }
  }
  </code></pre>
  
  <p><strong>View is not working, error below at input type and further:</strong></p>
  
  <pre><code>@model ShoppingCartViewModel
  @{
      ViewData["Title"] = "Index";
  }
  
  &lt;h2&gt;Edit Shopping Cart&lt;/h2&gt;
  
  &lt;h4&gt;edit&lt;/h4&gt;
  &lt;hr /&gt;
  &lt;div class="row"&gt;
      &lt;div class="col-md-4"&gt;
          @foreach (var item in Model.ShoppingCart)
          {
              &lt;form asp-action="Edit"&gt;
                  &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
                  &lt;input type="hidden" asp-for="modelItem=&gt;item.CartLineId" /&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label asp-for="modelItem=&gt;item.Product.ProductName" class="control-label"&gt;&lt;/label&gt;
                      &lt;input asp-for="modelItem=&gt;item.Product.ProductName" class="form-control" /&gt;
                      &lt;span asp-validation-for="modelItem=&gt;item.Product.ProductName" class="text-danger"&gt;&lt;/span&gt;
                  &lt;/div&gt;
  </code></pre>