Skip to main content

Binding CheckBoxList in ASP.Net MVC

Blogspot
Introduction
I would like to share how to bind the checkbox list in MVC. I will use a Model (a class file) to define various attributes for checkboxes.

For a basic understanding of MVC kindly use the following link:

ASP.NET MVC Overview

The following is the procedure.
1. Creating Model
We created a "SubjectModel" class under the Models folder and defined the following two properties:
  1. Subject: to display text
  2. Selected: to display check/uncheck
2. Creating the controller

  • We created a Controller "BindingCheckBoxController" under the controllers folder
  • We created an Action having the name = "DisplaycheckBoxes"
  • We created a list of Subjects (Subject model class)
  • Returning a list of subjects to the 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));  
    ViewData["Checkboxlist"]=listsubject;                                                                                                         
        return View();
}

 
3. Creating View
Right-click on "Action" and click on "Create view". 
4. Code at view
ere we have a view and we are trying to bind a Checkbox using Model properties.
  • At the highlighted code we are parsing the list of "SubjectModel" from ViewData["Checkboxlist"]) as in the following:
     
    <% foreach (var info in (IEnumerable<MVCtest.Models.SubjectModel>)ViewData["Checkboxlist"])

    We have passed Viewdata["Checkboxlist"] from the Controller (from the above, point 2.d).
     
  • We have used a foreach loop on the list of subjects.
  • We are using a HTML helper class to bind the Checkboxlist
5. Output
6. Binding checkboxes through strongly typed view

  • We have created a strongly typed view.
  • We have selected the "SubjectModel" class from the view data class dropdown.
  • We have made small changes while passing an object of the list to the view.
The following would be the updated code for the 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 a strongly typed view we have the following code.

The preceding line inherits "SubjectModel", which implies that the view is coupled with the Model.
 
Using a foreach loop as per the following, we can display a list of checkboxes using the DOM.

8. Output
We will get the same output as from point 5.
 
Conclusion
In this article we have learned how to bind a checkbox list using MVC2. We have learned two ways to bind checkboxes; using a strongly typed view and without using a strongly typed view.

References
I had posted another article for binding a Dropdown list using MVC2:

Comments

Contact Form

Name

Email *

Message *

Popular posts from this blog

Exporting DataTable to Excel in C# Using Interop

Introduction I would like to share a utility that can be used to export a datatable to Excel file using C#. We have various approaches to perform this activity. In ASP.NET we can do this by changing the Content type to xls but in C# .Net we might have various other approaches. Through this article we will learn how to export a data table to Excel using Interop. Code and Steps Here in this application we will use a Sample datatable and then learn how to export data to an Excel file. We will learn the following things in this article: Creating Excel file using C# Writing data to cells Formatting data to cells Working with Excel range 1. Adding References First we need to add a reference for Microsoft.office.interop.Excel as in the following: 2. Adding sample data table to the code Use the following code to add the sample data table:     static   DataTable  GetTable()     {       ...

Indexes In SQL SERVER in Hindi Part 1