k12 MVC approach custom checkbox list development

innovix solutions asked on July 10, 2019 12:45

Can you advise how we can develop custom multi check box list and validation for Kentico form development?

Kentcio form doesn't cover to multi checkboxes and validation rules are not there yet.

The difficult part for us is how to get option box data to binding them into checkbox list of custom component.

Recent Answers

Peter Mogilnitski answered on July 10, 2019 14:25

Can you give use an example of what you are trying to achieve? If you use a multiple choice form control and let say list of your check boxes: Image Text

if 2nd and 3nd checkboxes are selected your value will be 2|3, you can apply regex or for example if you want to have 1 least one checkbox selected, you can checked required checkbox and add validation rule that minimum text length is 1

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on July 10, 2019 19:37 (last edited on July 10, 2019 19:37)

In MVC, in order to get an array of values in a Form post, you need to have multiple controls with the same name. Example:

<input type="checkbox" name="MyCheckboxes" value="1"/>
<input type="checkbox" name="MyCheckboxes" value="2"/>
<input type="checkbox" name="MyCheckboxes" value="3"/>
<input type="checkbox" name="MyCheckboxes" value="4"/>

This will then work for catching int[] MyCheckboxes In the binding.

In your rendering of the Model's EditorFor, you may have to manually do it with a foreach loop through the available options (and which are already checked), but as long as you follow that naming schema, it should work. I ran into the same thing myself, although i haven't created a Form Component for this.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.