lundi 30 novembre 2015

ModelState validation fails when the CheckBox is unchecked

In the view:

@Html.CheckBoxFor(m => m.Insert_DSA.ChkFlag)<b>Check this if you want to change the password at your first login</b>

there's no validation of any sort for this checkbox done manually by me,but still when the correctly validated data is posted to the controller ModelState.IsValid property isfalse if the checkbox is unchecked. When i looked for the ModelState errors using

var errors = ModelState.Where(x => x.Value.Errors.Any()).Select(x => new { x.Key, x.Value.Errors });

it gives me

The ChkFlag field is required.

What I want: I don't want the checkbox to be a required field and I want ModelState.IsValid property to be true even when the checkbox is unchecked.




Aucun commentaire:

Enregistrer un commentaire