MVCCheckBoxList
MVCCheckBoxList is an extension for the MVC HtmlHelper class to create a POSTable checkbox list on a view, based on the data passed from the view model or defined locally.
Features
Earlier, when we need to create the checkbok list in MVC, we loop over the list of data in a model and use a checkbox within that loop.
And get the checked and unchecked data via jQuery or by using similar things.
A MVC CheckBoxlist has given us an easy way to create a checkboxlist without iterating over the list.
It creates POSTable checkboxes with the list provided from the model that is strongly typed with the model bound to the View so we do not need to use loops nor do we need to use jQuery to get the changed data any more.
How to install
Using Nuget packages we just need to use the following command in the Package Manager Console.
PM> Install-Package MvcCheckBoxList
Step 1: Open Visual Studio and select Tools >> Library Package Manager >> Package Manager Console.
The Package Manager Console window will appear as in the following:
Step 2: Type the command and hit Enter.
And the package is installed and added to your application.
How to use
Step 1: We need a set of data having the text and value .
For examle create a class.
- public class CheckboxListItems
- {
- public string CheckBoxText { get; set; }
- public string CheckBoxValue { get; set; }
- }
- public class MvcCheckBoxListModel
- {
- //All the Items to be displayed in check Box List
- public List<CheckboxListItems> AvailableItems { get; set; }
- //All the Selected Items to be marked as checked
- public List<CheckboxListItems> SelectedItems { get; set; }
- //all The Items which get checked on view
- public PostedItems PostedItem { get; set; }
- }
- public class PostedItems
- {
- public string[] ItemIds { get; set; }
- }
You can call a method to get the data from the database.
And pass the model to get it bound to the view.
- public ActionResult Index()
- {
- MvcCheckBoxListModel model = new MvcCheckBoxListModel();
- return View(model);
- }
- @Html.CheckBoxListFor(
- model => model.PostedItem.ItemIds,
- model => model.AvailableItems,
- item=>item.CheckBoxValue,
- item=>item.CheckBoxText,
- model=>model.SelectedItems,
- MvcCheckBoxList.Model.Position.Horizontal)

On submit you will get the selected CheckBoxIds in PostedItem.ItemIds of the Model.
Tanmay ThakurPosted May 8, 2015, 8:59 AM
hi tanmay, can you please share example of select all option for above checklist in jquery and how to add client event for same checkboxlist
M APosted Feb 2, 2015, 9:13 AM
this is really a nice article, very well explained...!!
Akinapelli KarthikPosted Jan 29, 2015, 3:51 PM
good one
Arweb AroshanzamirPosted Jan 29, 2015, 5:19 AM
thanks sir..very informative
Manish Kumar ChoudharyPosted Jan 29, 2015, 4:04 AM
Nice one..