We know that in MVC, a CheckBoxFor
will generate a checkbox
with a value="true"
and a hidden
with a value=false
. Both input controls will share the same name.
It is very reasonable because the form will be able to POST a false value if the box is unchecked. And the model binder will ignore the hidden input when the checkbox return a true.
But now i have overridden the form submit event in order to send the form data into a WebAPI controller in JSON format.
When serializing the form data, there is no mechanism to parse the relationship between the checkbox and the hidden correctly. Therefore, when unchecked, it returns a false
, which is okay. But when checked, it returns a {true, false}
instead of true
, because the serializeArray()
function goes through every input
and find two values goes to a same name.
The question is: What is the best way to correct it?