1

I have list of checkboxes on my view using html helper as below. They are not required field.:-

@Html.CheckBoxFor(m=> m.List1[0].myClass.myValue)
@Html.CheckBoxFor(m=> m.List1[1].myClass.myValue)
@Html.CheckBoxFor(m=> m.List1[2].myClass.myValue)
@Html.CheckBoxFor(m=> m.List1[3].myClass.myValue)

with myValue property defined as below in class :-

[XmlIgnore]
public bool myValue{ get; set; }

On form submission the I am getting "A value if required" error for each checkbox. On checking the html page in browser tool. I founf dollowing which seems to be issue: -

<input name="List1[0].myClass.myValue" type="hidden" value>

I am not sure why this value doesn't even set to false when checkboxes are left un-checked.How can i avoid this validation error of "A value is required".

tereško
  • 58,060
  • 25
  • 98
  • 150
user2232861
  • 273
  • 2
  • 10
  • 27

1 Answers1

0

When you put a checkbox in your view, you always get a hidden field with it, acting as a default value when your checkbox is unchecked. See those links for more informations :

In your code sample (the input element), value is not set, is that a typo ?

Also, you should have inputs with type="checkbox", do they appear correctly in your html ?

Community
  • 1
  • 1
Réda Mattar
  • 4,361
  • 1
  • 18
  • 19