title: how to bind true/false value to checkbox with ASP.NET MVC and Knockout JS
body: <p>I'm trying to bind the IsConfirmed field from the ASP.NET webpages_Membership table to a checkbox, using Knockout JS and can't seem to get it working.</p>
  
  <p>I have it binding to the checkbox, but unless I set it to !$data.IsConfirmed, the checkboxes are not check. I have also hooked up a click event to fire an updateUser function in my view-model, it does update it, but doesn't toggle the value sent. It always sends true, when it should send true if the checkbox is checked, otherwise false and then uncheck the checkbox.</p>
  
  <p>Here is my markup</p>
  
  <pre><code>&lt;table id="usersTable" class="table table-striped table-bordered table-hover"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;User ID&lt;/th&gt;
              &lt;th&gt;Username&lt;/th&gt;
              &lt;th&gt;Role&lt;/th&gt;
              &lt;th&gt;Date Created&lt;/th&gt;            
              &lt;th&gt;Last Failed Login&lt;/th&gt;            
              &lt;th&gt;Active&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody data-bind="foreach: users"&gt;
          &lt;tr&gt;            
              &lt;td&gt;
                  &lt;span data-bind="text: $data.UserId"&gt;&lt;/span&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;span data-bind="text: $data.Username"&gt;&lt;/span&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;span data-bind="text: $data.Role"&gt;&lt;/span&gt;
              &lt;/td&gt;            
              &lt;td&gt;
                  &lt;span data-bind="text: $data.DateCreated"&gt;&lt;/span&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;span data-bind="text: $data.LastFailedLogin"&gt;&lt;/span&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;input type="checkbox" data-bind="click: updateUser, checked: $data.IsConfirmed" /&gt;
              &lt;/td&gt;           
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>And the Javascript</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      var baseUri = '@ViewBag.ApiUrl';
      var self = this;
      self.users = ko.observableArray();
      // define user view-model
      function UsersViewModel() {               
          // get users to populate the view model        
          $.getJSON(baseUri, self.users);        
  
          // update the users IsConfirmed status
          self.updateUser = function (user) {            
              $.ajax({ type: "PUT", url: baseUri + '/' + user.UserId, data: user });
          }
          // re-load the users after any modifications
          $.getJSON(baseUri, self.users);        
      };   
      $(document).ready(function () {
          ko.applyBindings(new UsersViewModel());        
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>JSON From Server</strong></p>
  
  <pre><code>[{
      "UserId": 6,
      "Username": "john@jdoe.com",
      "Role": "Guest",
      "DateCreated": "11/12/2012 1:18:41 PM",
      "LastFailedLogin": "",
      "IsConfirmed": false
  }, {
      "UserId": 3,
      "Username": "simpleuser",
      "Role": "Administrator",
      "DateCreated": "11/11/2012 6:17:32 PM",
      "LastFailedLogin": "",
      "IsConfirmed": false
  }, {
      "UserId": 2,
      "Username": "testUser",
      "Role": "Administrator",
      "DateCreated": "11/11/2012 6:17:32 PM",
      "LastFailedLogin": "",
      "IsConfirmed": false
  }, {
      "UserId": 5,
      "Username": "jdoe1@nowhere.com",
      "Role": "Student",
      "DateCreated": "11/12/2012 3:31:38 AM",
      "LastFailedLogin": "",
      "IsConfirmed": false
  }, {
      "UserId": 8,
      "Username": "nobody@somewhere.com",
      "Role": "Alumni",
      "DateCreated": "11/12/2012 1:24:15 PM",
      "LastFailedLogin": "",
      "IsConfirmed": false
  }, {
      "UserId": 9,
      "Username": "test@test.com",
      "Role": "Applicant",
      "DateCreated": "11/12/2012 1:25:18 PM",
      "LastFailedLogin": "",
      "IsConfirmed": false
  }, {
      "UserId": 1,
      "Username": "user1",
      "Role": "Administrator",
      "DateCreated": "11/11/2012 6:17:31 PM",
      "LastFailedLogin": "11/12/2012 4:54:51 PM",
      "IsConfirmed": false
  }, {
      "UserId": 4,
      "Username": "test@test.com",
      "Role": "Alumni",
      "DateCreated": "11/11/2012 6:22:23 PM",
      "LastFailedLogin": "11/11/2012 8:23:57 PM",
      "IsConfirmed": false
  }, {
      "UserId": 7,
      "Username": "test@test2.com",
      "Role": "Student",
      "DateCreated": "11/12/2012 1:23:56 PM",
      "LastFailedLogin": "",
      "IsConfirmed": false
  }]
  </code></pre>
  
  <p><strong>C# Web API Controller</strong></p>
  
  <pre><code>// GET api/Membership
          public IEnumerable&lt;SiteMemberDTO&gt; GetMemberships()
          {
              var users = db.webpages_Membership.AsEnumerable();
              var profiles = db.UserProfiles.AsEnumerable();            
  
              // now build our DTO object
              var membersList = new List&lt;SiteMemberDTO&gt;();
              foreach (var profile in profiles)
              {
                  var member = new SiteMemberDTO() {
                      UserId = profile.UserId, 
                      Username = profile.UserName,
                      Role = profile.webpages_Roles.SingleOrDefault(r =&gt; r.RoleName != "").RoleName,
                      DateCreated = db.webpages_Membership.Find(profile.UserId).CreateDate.ToString(),
                      LastFailedLogin = db.webpages_Membership.Find(profile.UserId).LastPasswordFailureDate.ToString()
                  };
  
                  membersList.Add(member);
              }
              return membersList;
          }
  </code></pre>
  
  <p><strong>DTO (Data-Transfer-Object)</strong></p>
  
  <pre><code> public class SiteMemberDTO
      {
          public int UserId { get; set; }
          public string Username { get; set; }
          public string Role { get; set; }
  
          public string DateCreated { get; set; }
          public string  LastFailedLogin { get; set; }
  
          public bool IsConfirmed { get; set; }
      }
  </code></pre>
  
  <p>This is the complete code for what I'm doing. For some reason I see in the JSON it's returning false for <strong>IsConfirmed</strong>, when in the database it's true for every record.</p>