maxatwork/form2js — Issue #35: Asp.Net MVC checkboxes special case
Author: michael-lang
Created: 2012-02-21 04:43:19+00:00
State: open

When an Asp.net MVC 3 view helper spits out a checkbox it puts a checkbox and a hidden text field in the html. This is to ensure that a value is passed back in a post even when the checkbox is not checked. The model binder on the server needs all the properties to have a value to ensure the proper model is bound, and thus the proper controller action method is invoked.

```html

```

So now the name/value pairs that are generated contain two values for the check box when it is checked, true and false. And false is the second value. So later in processNameValues the last value in wins.

A possible solution is to filter out duplicate values for check boxes. here is the relevant jQuery code I came up with. This generates my name/value list that I then pass into a modified version your processNameValues function that takes in a json object to update instead of creating a new one. See issue #34.

```javascript
function updateJson(form, json) {
var arr = $(form).find(":input, :radio")
.filter(function () {
return this.name && !this.disabled &&
(this.checked || /select|textarea/i.test(this.nodeName) ||
/text|hidden|password|search/i.test(this.type));
})
.map(function (i, elem) {
var val = jQuery(this).val();
return val == null ?
null : { name: elem.name, value: val }
}).get();
$(form).find("input:checkbox")
.filter(function () {
return this.name;
})
.each(function(index, elem) {
arr = $.grep(arr, function(item, index){
return elem.name != item.name;
});
arr.push({ name: elem.name, value: (elem.checked ? "true" : "false") });
});
return processNameValues(json, arr, true, false);
}
```