Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 1.74 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Working with checkboxes in asp.net mvc3 plus mass update
  2. @model List<LeagueCounters.Models.champion>
  3. <form name="setFree id="setFree" method="POST" action="/Champion/SetFree">
  4.     @foreach (var item in Model)
  5.     {
  6.         if (item.isFree == true)
  7.         {
  8.            <input type="checkbox" id="@item.id" checked="checked" /> @Html.DisplayFor(modelItem => item.displayName)  
  9.         }
  10.         else
  11.         {
  12.             <input type="checkbox" id="@item.id" /> @Html.DisplayFor(modelItem => item.displayName)
  13.         }
  14.     }
  15. <p><input type="submit" value="Save" /></p>
  16.        
  17. [HttpPost, Authorize(Roles = "Admin")]
  18. public ActionResult SetFree(FormCollection fcMain)
  19. {
  20.      var sortedList = from c in _db.champions
  21.                       orderby c.name
  22.                       select c;
  23.      int counter = 0;
  24.      foreach (champion champ in sortedList)
  25.      {
  26.          if (fcMain[counter].Contains("true"))
  27.             champ.isFree = true;
  28.          else
  29.             champ.isFree = false;
  30.  
  31.          _db.champions.Attach(champ);
  32.          _db.ObjectStateManager.ChangeObjectState(champ, EntityState.Modified);
  33.  
  34.          counter++;
  35.      }
  36.  
  37.      _db.SaveChanges();
  38.  
  39.      return View();
  40. }
  41.        
  42. fcMain[counter].Contains("true")
  43.        
  44. <input type="hidden" value="false" id="@item.id" name="@item.id"/>
  45.        
  46. if (item.isFree == true)
  47. {
  48.    <input type="checkbox" id="@item.id"  checked="checked" /> @Html.DisplayFor(modelItem => item.displayName)  
  49. }
  50. else
  51. {
  52.    <input type="checkbox" id="@item.id" /> @Html.DisplayFor(modelItem => item.displayName)
  53. }
  54.        
  55. <input type="checkbox" id="@item.id" name="@item.id" @(!Item.isFree?"checked="checked":""") /> @Html.DisplayFor(modelItem => item.displayName)
  56.        
  57. _db.champions.Attach(champ);
  58. _db.ObjectStateManager.ChangeObjectState(champ, EntityState.Modified);