I'm struggling with Html.Checkbox in ASP.NET MVC. Imagine an Employee with a repeating group of Children:
alt text http://img220.imageshack.us/img220/7208/deletechildrensnapshotk.png
The "Add Child" button works fine, but I can't reliably use "Delete selected children". I render the checkboxes with this:
<% int i = 0; %>
<% foreach (var item in Model.Children) { %>
<tr>
<td>
<%=Html.CheckBox("childrenToDelete[" + i + "]", false, new {value = item.Name})>
</td>
</tr>
<% i++; %>
<% } %>
Here's how my controller action gets the list of children to delete from a FormCollection:
var childrenToDelete = new List<string>();
UpdateModel(childrenToDelete, "childrenToDelete");
I then create an object using the ViewModel pattern which contains the Employee and a List of Child. I can't figure out why 85% of the time I then throw an exception in the View at the Html.Checkbox line. About 15% of the time, it works fine. The exception is "The parameter conversion from type 'System.String' to type 'System.Boolean' failed." IE then displays:
- FormatException: String was not recognized as a valid Boolean.
- FormatException: Bobby is not a valid value for Boolean.
Of course "Bobby" is not a Boolean, so it fails. Any clue about why Html.Checkbox is trying to use "Bobby" as a Boolean? The same ViewModel works fine for adding children, so I don't think I have an error there.