title: asp.net mvc update multiple records
body: <p>is it possible to have a view for editing multiple records, in the same way that the index.cshtml view loops through records to display them (as below)?</p>
  
  <pre><code>@foreach (var item in Model) {
  &lt;tr&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.tvid)
      &lt;/td&gt;
  </code></pre>
  
  <p>So for each row above, it would relate to a different row in the database.</p>
  
  <p>Does anyone know of any examples showing how this may be achieved?</p>
  
  <p>Thanks for any pointers,</p>
  
  <p>Mark</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Model:</p>
  
  <pre><code> using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
  
  namespace MvcObjectives.Models
  {
  public class objectives
  {
      public int ID { get; set; }
      public int tvid { get; set; }
      public string tlnt { get; set; }
      public DateTime month { get; set; }
  
      public string objective  { get; set; }
      public int score { get; set; }
      public int possscore { get; set; }
      public string comments { get; set; }
   }
  </code></pre>
  
  <p>}</p>
  
  <p>Controller:</p>
  
  <pre><code> [HttpPost]
      public ActionResult Edit(objectives objectives)
      {
          if (ModelState.IsValid)
          {
              db.Entry(objectives).State = EntityState.Modified;
              foreach (objective Objective in objectives.objective)
  
              { }
  
              db.SaveChanges();
              return RedirectToAction("Index");
          }
          return View(objectives);
      }
  </code></pre>
  
  <p>I'm stick with the controller, if anyone can offer any assistance?</p>
  
  <p>Thanks again,</p>
  
  <p>Mark</p>
  
  <p><strong>2nd Update</strong></p>
  
  <p>The GET Controller to send the records to the view is:</p>
  
  <pre><code>        // GET: /Objective/Edit/
          public ActionResult Edit()
          {
              return View(db.objectives.ToList());
          }
  </code></pre>
  
  <p>The POST controller (where the values are posted back from the view) is:</p>
  
  <pre><code>        // POST: /Objective/Edit/
          [HttpPost]
          public ActionResult Edit(List&lt;objectives&gt; objectives)
          {
              if (ModelState.IsValid)
              {
           // the next part is where I am stuck - how to loop through the returned objectives, and update the records in the database
                  db.Entry(objectives).State = EntityState.Modified;
                  foreach (objectives obj in objectives)
                  {
                      var tempObj = (from objv in db.objectives
                                     where objv.ID==obj.ID
                                     select objv).First();
                }
  
                //  to do - how to save the updates sent back????
  
                  return RedirectToAction("Index");
              }
              return View(objectives);
          }
  </code></pre>