title: Allow user to edit list items in MVC?
body: <p>I'm using Entity Framework Core to build a simple web app. For this app, I've created a model called Company that includes basic business info + a list of contacts (sales reps).</p>
  
  <p>Here's my model: </p>
  
  <pre><code>public class Company
  {
      [Key]
      public int ID { get; set; }
      public string Name { get; set; }
      public string Promo { get; set; } 
      public virtual List&lt;Contact&gt; Contacts { get; set; }       
  }
  
  public class Contact
  {
      [Key]
      public int ContactID { get; set; }
      [ForeignKey("Company")]
      public int CompanyID { get; set; }
      public virtual Company Company { get; set; }
      public string ContactName { get; set; }
      public string ContactNumber { get; set; }
  }
  </code></pre>
  
  <p>Here's the controller's index() method:</p>
  
  <pre><code>// GET: Companies
      public async Task&lt;IActionResult&gt; Index()
      {
          List&lt;Company&gt; viewModelData = await _context.Companies
              .Include(c =&gt; c.Contacts)
              .ToListAsync();
          return View(viewModelData);
      }
  </code></pre>
  
  <p>Edit method: </p>
  
  <pre><code> // GET: Companies/Edit/5
          public async Task&lt;IActionResult&gt; Edit(int? id)
          {
              if (id == null)
              {
                  return NotFound();
              }
  
              var company = await _context.Companies
                  .Include(v =&gt; v.Contacts)
                  .FirstOrDefaultAsync(m =&gt; m.ID == id);
              if (company == null)
              {
                  return NotFound();
              }
              return View(company);
          }
  
          // POST: Companies/Edit/5
  
          [HttpPost]
          [ValidateAntiForgeryToken]
          public async Task&lt;IActionResult&gt; Edit(int? id, [Bind("ID,Name,Promo,Contacts")] Company company)
          {
              if (id == null)
              {
                  return NotFound();
              }
  
              var companyToUpdate = await _context.Companies
                  .Include(v =&gt; v.Contacts)
                  .FirstOrDefaultAsync(m =&gt; m.ID == id);
              if (await TryUpdateModelAsync&lt;Company&gt;(
                  companyToUpdate,
                  "",
                  i =&gt; i.Name, i =&gt; i.Promo, i =&gt; i.Contacts
                  )) { 
                  try
                  {
                      await _context.SaveChangesAsync();
                  }
                  catch (DbUpdateException /* ex */)
                  {
                      //Log the error (uncomment ex variable name and write a log.)
                      ModelState.AddModelError("", "Unable to save changes. " +
                          "Try again, and if the problem persists, " +
                          "see your system administrator.");
                  }
              return RedirectToAction("Index");
          }
           return View(companyToUpdate);
      }
  </code></pre>
  
  <p>This is not correct since the code only allows me to edit Company info. How do I modify the code so that I can edit both Company &amp; its contacts on the same edit view?</p>