title: mvc4 checkbox update database
body: <p>I had taken an example from somewhere on the internet (I cant remember where it was) and have placed it into my application. The code works but not exactly to how I want it and I cannot seem to figure out how to complete it.</p>
  
  <p>The code below takes all checked checkboxes and updates the database in the location it should. Great! However when you uncheck a checkbox there is no way of changing the field in the database from on to off.</p>
  
  <p>I suspect I will have to do a complete code rewrite however here is what I have at present.</p>
  
  <p>View:</p>
  
  <pre><code>  @foreach (var item in Model)
          {
              &lt;tr&gt;
                  @Html.HiddenFor(modelItem =&gt; item.id)
  
                  &lt;td class="selectAll"&gt;
                      &lt;input type="checkbox" name="complete" value="@item.id"/&gt;
  
                  &lt;/td&gt;
              &lt;/tr&gt;
           }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code> [HttpPost]
      public ActionResult Complete(FormCollection c)
      {
  
          int i = 0;
          if (ModelState.IsValid)
          {
  
              var DelIDArray = c.GetValues("item.id");
              var DelCompleteArray = c["complete"];
  
              for (i = 0; i &lt; DelIDArray.Count(); i++)
              {
                  string find = DelIDArray[i].ToString();
                  deliverylist dels = db.deliverylists.Find(Convert.ToInt32(DelIDArray[i]));
                  dels.is_complete = Convert.ToInt32(DelCompleteArray[i]);
                                 }
              db.SaveChanges();
          }
          return RedirectToAction("Complete");
  
      }
  </code></pre>