title: How to update multiple rows at once from a View (ASP.NET - Core)
body: <p>I'm trying to create an view in which multiple values can be edited at once. The view consists of multiple rows from a List in which I want to check a checkbox to change the value of the boolean <code>EnableAlertsUpload</code> to <code>True</code> or <code>False</code>, as can be seen in the following picture:</p>
  <p><a href="https://i.stack.imgur.com/9CCQu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9CCQu.png" alt="enter image description here" /></a></p>
  <p>When the Update button gets pressed the <code>IActionResult Edit</code> should be called which updates all the <code>EnableAlertsUpload</code> booleans in the database. The database is made using the Code First approach with Entity Framework Core.</p>
  <p>When i press the Update button right now a new record will be created and all values are set to <code>NULL</code>. So it seems there is an issue with my <code>SaveDevice</code> method in the Repository</p>
  <p><strong>AlertsController</strong></p>
  <pre><code>    public ViewResult Devices()
          =&gt; View(new DevicesViewModel
          {
              Devices = repo.getDevices()
          });
  
      [HttpPost]
      public IActionResult Edit(Devices devices)
      {
          repo.SaveDevice(devices);
  
          return RedirectToAction();
      }
  </code></pre>
  <p><strong>Repository</strong></p>
  <pre><code>    public void SaveDevice(Devices devices)
      {
          if (devices.objid == 0)
          {
              db.Devices.Add(devices);
          }
          else
          {
              Devices dbEntry = db.Devices
                  .FirstOrDefault(p =&gt; p.objid == devices.objid);
              if (dbEntry != null)
              {
                  dbEntry.objid = devices.objid;
                  dbEntry.device = devices.device;
                  dbEntry.group = devices.group;
                  dbEntry.host = devices.host;
                  dbEntry.EnableAlertsUpload = devices.EnableAlertsUpload;
              }
          }
          db.SaveChanges();
      }
  </code></pre>
  <p><strong>Devices</strong></p>
  <pre><code>public partial class Devices
  {
      [Key()]
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public int objid { get; set; }
      public string group { get; set; }
      public string device { get; set; }
      public string host { get; set; }
      public bool EnableAlertsUpload { get; set; }
  }
  </code></pre>
  <p><strong>DevicesViewModel</strong></p>
  <pre><code>public class DevicesViewModel : PageModel
  {
      [BindProperty]
      public List&lt;Devices&gt; Devices { get; set; }
  }
  </code></pre>
  <p><strong>Devices View</strong></p>
  <pre><code>@model Models.ViewModels.DevicesViewModel  
    
  &lt;form asp-action=&quot;Edit&quot; method=&quot;post&quot;&gt;
      &lt;table class=&quot;table&quot;&gt;
          &lt;tr&gt;
              &lt;th&gt;Id&lt;/th&gt;
              &lt;th&gt;Device&lt;/th&gt;
              &lt;th&gt;Group&lt;/th&gt;
              &lt;th&gt;Host&lt;/th&gt;
              &lt;th&gt;Enabled Alerts Update&lt;/th&gt;
          &lt;/tr&gt;
          @for (var i = 0; i &lt; Model.Devices.Count(); i++)
          {
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;input type=&quot;hidden&quot; asp-for=&quot;Devices[i].objid&quot; /&gt;
                      @Model.Devices[i].objid
                  &lt;/td&gt;
                  &lt;td&gt;@Model.Devices[i].device&lt;/td&gt;
                  &lt;td&gt;@Model.Devices[i].group&lt;/td&gt;
                  &lt;td&gt;@Model.Devices[i].host&lt;/td&gt;
                  &lt;td&gt;&lt;input asp-for=&quot;Devices[i].EnableAlertsUpload&quot;/&gt;&lt;/td&gt;
              &lt;/tr&gt;
          }
      &lt;/table&gt;
      &lt;button input type=&quot;submit&quot; value=&quot;Edit&quot;&gt;Update&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>