I would like to share the way to bind the checkbox list in MVC. I will use Model (a

class file) to define various attribute for checkboxes.

For basic knowledge of MVC kindly follow link below

Following are the Steps.

1)      Creating Model.

Binding Checkbox in MVC2

We created a “SubjectModel” class under Models folder and defined two properties

a)      Subject : to Display text

b)      Selected : to display check/uncheck

2)      Creating controller

Binding Checkbox in MVC2

a)      We created a Controller “BindingCheckBoxController” under controllers folder

b)      We created an Action having name = “DisplaycheckBoxes”

c)       We created a list of Subject (Subject model class)

d)        Returning List of subject to View.

  public ActionResult DisplayCheckboxes()
            List<SubjectModel> listsubject = new List<SubjectModel>();
            listsubject.Add(new SubjectModel("Physics",true));
            listsubject.Add(new SubjectModel("Chemistry",true));
            listsubject.Add(new SubjectModel("History",false));
            listsubject.Add(new SubjectModel("Maths",false));
            listsubject.Add(new SubjectModel("Boilogy",false));
    listsubject.Add(new SubjectModel("Hindi",true));  
            return View();


3)       Creating View .

Right click on Action and Click on create view. Following screen will display.

Binding Checkbox in MVC2 

a)       Click on add button and View will be created. Here we are not creating strongly typed view. 

4)       Code at view.

Here we have view and we are trying to bind Checkbox using Model properties.

Binding Checkbox in MVC2 

a)      At highlighted code we are parsing list of “SubjectModel” from ViewData["Checkboxlist"]). 

<% foreach (var info in (IEnumerable<MVCtest.Models.SubjectModel>)ViewData["Checkboxlist"])

We have passed Viewdata[“Checkboxlist”] from Controller. (From above, point 2.d)) 

b)      We have used foreach loop on list of subjects.

c)       We are using html helper class to bind Checkboxlist 

5)      Output

Binding Checkbox in MVC2
6)      Binding checkboxes through strongly typed view 
Binding Checkbox in MVC2

a)      We have created strongly typed view.
b)      We have selected “SubjectModel” class from view data class dropdown
c)       We have done small changes while passing object of list to view.

Following would be updated code for Action
public ActionResult DisplayCheckboxes ()
           List<SubjectModel> listsubject = new List<SubjectModel>();
           listsubject.Add(new SubjectModel("Physics", true));
           listsubject.Add(new SubjectModel("Chemistry", true));
           listsubject.Add(new SubjectModel("History", false));
              listsubject.Add(new SubjectModel("Maths", false));
           listsubject.Add(new SubjectModel("Boilogy", false));
           listsubject.Add(new SubjectModel("Hindi", true));
           return View(listsubject);

7)      Code at view.

For strongly typed view we have following code.

Binding Checkbox in MVC2

The above line inherits “SubjectModel”, which implies that view is coupled with Model. 

Using foreach loop as per below we can display list of checkbox at DOM.

Binding Checkbox in MVC2 

8)      Output

We will get same output as per point 5


In this article we have learned how to bind checkbox list using MVC2. We have

learned two ways to bind checkboxes using strongly typed view and without using

strongly typed view.


I had posted another article for binding Dropdown list using MVC2 

Happy Coding!!!!!!!


  Modified On Sep-07-2019 12:14:42 AM

Leave Comment