So by default @Html.CheckBoxFor
is going to generate a hidden field after the original field, something like
<input name="xxx" id="xxx" type="checkbox" .... data-val="true" value="true"/>
<input name="xxx" type="hidden" value="false"/>
When I do ajax submit with $(form).serialize()
the post body would contain two xxx fields if the checkbox is checked. (xxx=true&&xxx=false
) This is expected. But then ModelState.IsValid
returns false and the error is
System.InvalidOperationException: The parameter conversion from type 'System.Collections.Generic.List`1[System.String]' to type 'System.Boolean' failed because no type converter can convert between these types
How can I fix this??
Update: It is happening to my WebAPI controller, I am doing the regular model binding with
[HttpPost]
public void MyMethod(MyModel model)
{
if ( !ModelState.IsValid )
throw new HttpResponseException( HttpStatusCode.BadRequest );
}