In this article we will learn how to bind CheckBoxList in ASP.NET MVC used to choose multiple records. So lets start with step by step approach.
Step 1: Create an MVC Application.
Now let us start with a step by step approach from the creation of simple MVC application as in the following:
- "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
- "File", then "New" and click "Project..." then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click on OK .
Right click on model folder of created MVC application project and add class named CheckBoxModel.cs as:
CheckBoxModel.cs
- public class CheckBoxModel
- {
- public int Value { get; set; }
- public string Text { get; set; }
- public bool IsChecked { get; set; }
- }
- public class CheckBoxList
- {
- public List<CheckBoxModel> CheckBoxItems { get; set; }
- }
Right click on Controllers folder of created MVC application and add Controller named HomeController.cs as:
HomeController.cs
- public class HomeController : Controller
- {
- // GET: Home
- public ActionResult Index()
- {
- //Creating object of CheckBoxList model class
- CheckBoxList ChkItems = new CheckBoxList();
- //Additng items to the list
- List<CheckBoxModel> ChkItem = new List<CheckBoxModel>()
- {
- new CheckBoxModel {Value=1,Text="ASP.NET",IsChecked=true },
- new CheckBoxModel {Value=1,Text="C#",IsChecked=false },
- new CheckBoxModel {Value=1,Text="MVC",IsChecked=false },
- new CheckBoxModel {Value=1,Text="Web API" ,IsChecked=false},
- new CheckBoxModel {Value=1,Text="SignalR",IsChecked=false },
- new CheckBoxModel {Value=1,Text="SQL" ,IsChecked=false},
- };
- //assigning records to the CheckBoxItems list
- ChkItems.CheckBoxItems = ChkItem;
- return View(ChkItems);
- }
- }
Right click on Views folder of created MVC application project and add empty View name Index.cshtml as:
Now open the Index.cshtml view and write the following code into the view
Now open the Index.cshtml view and write the following code into the view
- @model BindCheckBoxListInMVC.Models.CheckBoxList
- @{
- ViewBag.Title = "www.compilemode.com";
- }
- <div class="form-horizontal">
- <h4>Select your favourite Subjects</h4>
- @foreach (var item in Model.CheckBoxItems)
- {
- <input id="chk@(item.Value)"
- type="checkbox"
- value="@item.Value"
- checked="@item.IsChecked" />
- @item.Text <br />
- }
- </div>

From all the above examples we learned how to bind CheckBoxList in ASP.NET MVC.
Note
- Do proper validation such as date input values while implementing.
- Download the Zip file of the sample application for a better understanding
Summary
I hope this article is useful for all readers, if you have a suggestion, then please contact me.
I hope this article is useful for all readers, if you have a suggestion, then please contact me.


Andrei MarcelPosted Apr 6, 2021, 4:01 PM EDT
Sir, how do you save the data in POST method ? Thank you in advance !
MIke HPosted Apr 6, 2021, 3:45 PM EDT
Not a good read. Doesn't show how to bind to model on submit.
Reina varasPosted Jun 26, 2018, 6:18 PM EDT
Great Article!!
Vithal WadjeTOP 100Posted Oct 27, 2015, 9:10 AM EDT
Thanks Pc Patel sir
Vithal WadjeTOP 100Posted Oct 27, 2015, 9:10 AM EDT
Thanks Sibeesh Venu sir
Phoolchand PatelPosted Oct 26, 2015, 7:04 AM EDT
Nice article Vithal sir
Sibeesh VenuTOP 100Posted Oct 25, 2015, 9:19 PM EDT
Nice Share
Vithal WadjeTOP 100Posted Oct 25, 2015, 9:16 PM EDT
Thanks Harshad Pansuriya sir
Vithal WadjeTOP 100Posted Oct 25, 2015, 9:16 PM EDT
Thanks Nilesh Jadav sir
Harshad PansuriyaPosted Oct 25, 2015, 8:31 PM EDT
Nice one
Manoj KulkarniPosted Oct 25, 2015, 7:16 PM EDT
Nice article. Thank you for sharing
Mohammed IbrahimPosted Oct 25, 2015, 6:59 PM EDT
nice
Nilesh JadavPosted Oct 25, 2015, 5:43 PM EDT
Thank you sir ! Helped me in binding checkbox
Vithal WadjeTOP 100Posted Oct 25, 2015, 10:26 AM EDT
Thanks Ravi Patel sir
Vithal WadjeTOP 100Posted Oct 25, 2015, 10:25 AM EDT
Thanks Santhakumar Munuswamy sir
Ravi PatelPosted Oct 25, 2015, 7:46 AM EDT
nice article
Santhakumar MunuswamyPosted Oct 25, 2015, 6:31 AM EDT
Good Article