This project is read-only.

CheckBox list for selecting items 

The CheckBoxList control allows items in a list to be chosen by selecting CheckBoxes instead of using a Multiselect DropDown or ListView.  

Suppose the View Model has a prpoperty Roles,  containing the codes of all roles assigned to a user. The Role property can be any IEnumerable with an Add method.  Moreovere, suppose for sack of simplicity, that a list of all roles is contained in the static IEnumerable property,  MVCNestedModels.Models.RegisterModel.AllRoles whose items contains a Code and a Name property with respectively the role code and the role name.

In order to let the user select the roles with the CheckListBox control you need as a first step to create a ChoiceList data structure from the Role IEnumerable with the instruction:

new ChoiceList<RoleInBlog, int, string>( RegisterModel.AllRoles,(t => t.Code), (t => t.Name))

The two delegates (t  => t.Code) and (t  => t.Code) select the fields to be used repectively as value field and as display field.

However, if we simply create the ChoiceList class with a new, we have to specify all generics arguments, since in this case Visual Studio is not able to infer them automatically. For this reason, we defined the helper method ChoiceListHelper.Create:


ChoiceListHelper.Create( RegisterModel.AllRoles,(t => t.Code), (t => t.Name))


Then you can pass the ChoiceList to the CheckBoxListFor Helper in your View, yielding:


@Html.CheckBoxListFor(m => m.Roles,
              RegisterModel.AllRoles, (t => t.Code), (t => t.Name))



@Html.CheckBoxListFor(m => m.Roles,
        ChoiceListHelper.Create(RegisterModel.AllRoles,(t => t.Code),(t => t.Name)),
                         true, "RoleListTemplate" )  


The first code snippet displays the CheckBox list with a standard template while the second one displays the CheckBox list with the custom template "RoleListTemplate".

The "RoleListTemplate" ViewModel is the whole list of items contained in the CheckBoxList<IEnumerable<TValue>, TChoiceItem, TValue, TDisplay> data structure, that in our example becomes CheckBoxList<IEnumerable<int>, RoleInBlog, int, string>


Instead, of using a Template for all Items together, one can also use the itemTemplate optional parameter  that can accept a template to be used for each single item of the list. In our case the item template has been passed as an in-line Lambda Expression, with the help of the Syntactic Sugar class. For more information about the use of templates, please see here.

        m => m.Roles1,
        ChoiceListHelper.Create(RegisterModel.AllRoles, m => m.Code, m => m.Name),
                    itemTemplate: _S.L<CheckBoxListItem<int>>(
                        (x) => string.Format(
                            x.CheckBoxFor(m => m.Selected).ToString(),
                            x.DisplayFor(m => m.Label).ToString())



Both the ChoiceList constructor and the ChoiceListHelper.Create method have also an overload with two more arguments.These two extra arguments are two delegates that, when specified, are called on each CheckBoxListItem to get respectively the CheckBox  and the CheckBox label Html attributes. This way, each item may have a look that depends on the data it contains.

Last edited Jun 22, 2014 at 11:49 AM by frankabbruzzese, version 17


No comments yet.