Introduction
I have seen many developers get stuck using Checkboxlists in MVC and retrieving values on post. So to make it simple I came up with a simple example of these controls.
Agenda
- Creating Model for Checkbox control
- Creating Controller
- Creating View
- Getting checked values of checkbox in Controller Action Method.
Let's start with creating a simple project in MVC 4 by selecting Basic Project template and naming it “MvcCheckbox”.
After adding the project the following is a folder view of the project that was created.
![]()
After adding the project let's add a Model.
Adding Model
To add model just right-click the Models folder then select Add then select Class. An Add New Item dialog will popup asking for the class name. Name it “CheckModel” and click on the Add button.
![]()
![]()
The following is the simple code that was generated by adding the model.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace MvcCheckbox.Models
- {
- public class CheckModel
- {
-
- }
- }
Now to add properties to CheckModel.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace MvcCheckbox.Models
- {
- public class CheckModel
- {
- public int Id
- {
- get;
- set;
- }
- public string Name
- {
- get;
- set;
- }
- public bool Checked
- {
- get;
- set;
- }
- }
- }
After adding Model now let's add a Controller.
Adding Controller
To add a new Controller just right-click the Controller folder then select Add then select Controller. An Add Controller dialog will popup asking for the Controller name. Name it “HomeController” and then in the Scaffolding option : Template select “Empty MVC controller“. Finally click on the Add button.
![]()
Configuring Controller
![]()
The following is the simple Controller code that was generated.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace MvcCheckbox.Controllers
- {
- public class HomeController : Controller
- {
-
-
-
- public ActionResult Index()
- {
- return View();
- }
-
- }
- }
Now let's create a list of Checkmodels in the Index Action Method and pass it to the view.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcCheckbox.Models;
-
- namespace MvcCheckbox.Controllers
- {
- public class HomeController : Controller
- {
-
- [HttpGet]
- public ActionResult Index()
- {
- var list = new List<CheckModel>
- {
- new CheckModel{Id = 1, Name = "Aquafina", Checked = false},
- new CheckModel{Id = 2, Name = "Mulshi Springs", Checked = false},
- new CheckModel{Id = 3, Name = "Alfa Blue", Checked = false},
- new CheckModel{Id = 4, Name = "Atlas Premium", Checked = false},
- new CheckModel{Id = 5, Name = "Bailley", Checked = false},
- new CheckModel{Id = 6, Name = "Bisleri", Checked = false},
- new CheckModel{Id = 7, Name = "Himalayan", Checked = false},
- new CheckModel{Id = 8, Name = "Cool Valley", Checked = false},
- new CheckModel{Id = 9, Name = "Dew Drops", Checked = false},
- new CheckModel{Id = 10, Name = "Dislaren", Checked = false},
-
- };
- return View(list);
- }
-
- }
- }
Adding View
To add the view just right-click anywhere inside the Action Method Index and select Add.
The Add view dialog will popup with the default View name the same as the Action Method Name.
![]()
The following is the Add View dialog.
![]()
After seeing the dialog just click on the Add button.
Simple Index.cshtml will be created.
![]()
Some default code will be generated after adding the View.
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
Now let's add a tightly coupled model to the view.
In this I have created a List of CheckModels and took three controls to display the checkbox.
Note
- HiddenFor: Hiddenfor maintains the ID.
- DisplayFor: DisplayFor displays the checkbox name (text).
- CheckboxFor: CheckboxFor displays the checkbox.
I am iterating these controls to create a unique id for each control.
- @model List<MvcCheckbox.Models.CheckModel>
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
-
- @using (Html.BeginForm())
- {
- for (var i = 0; i < Model.Count(); i++)
- {
- <table>
- <tr>
- <td>
- @Html.HiddenFor(it => it[i].Id)
- @Html.DisplayFor(it => it[i].Name)
- </td>
- <td>
- @Html.CheckBoxFor(it => it[i].Checked,new {Style ="vertical- align:3px}"})
- </td>
- </tr>
- </table>
-
- }
-
- <input id="Submit1" type="submit" value="submit" />
-
- }
Now let's run the application and view the Checkbox Control.
To call it just type this URI in your browser window with your localhost port.
http://localhost:##### /Home/index
Output
![]()
Adding New Action Method with Action Selector [HttpPost]
Now let's finally add a new Action Method Post for getting values.
- [HttpPost]
- public ActionResult Index(List<CheckModel> list)
- {
- return View(list);
- }
Complete view of Controller after adding both Action selector
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcCheckbox.Models;
-
- namespace MvcCheckbox.Controllers
- {
- public class HomeController : Controller
- {
-
- [HttpGet]
- public ActionResult Index()
- {
- var list = new List<CheckModel>
- {
- new CheckModel{Id = 1, Name = "Aquafina", Checked = false},
- new CheckModel{Id = 2, Name = "Mulshi Springs", Checked = false},
- new CheckModel{Id = 3, Name = "Alfa Blue", Checked = false},
- new CheckModel{Id = 4, Name = "Atlas Premium", Checked = false},
- new CheckModel{Id = 5, Name = "Bailley", Checked = false},
- new CheckModel{Id = 6, Name = "Bisleri", Checked = false},
- new CheckModel{Id = 7, Name = "Himalayan", Checked = false},
- new CheckModel{Id = 8, Name = "Cool Valley", Checked = false},
- new CheckModel{Id = 9, Name = "Dew Drops", Checked = false},
- new CheckModel{Id = 10, Name = "Dislaren", Checked = false},
-
- };
- return View(list);
- }
-
- [HttpPost]
- public ActionResult Index(List<CheckModel> list)
- {
- return View(list);
- }
-
- }
- }
Now let's test by checking the checkbox and posting values to the controller.
I have checked the Aquafina , Bisleri and Himalayan values. I should get the value marked true in the Controller.
Output: View with Checked checkboxes
![]()
Finally retrieve the values after posting the values from the View to the Controller.
Here I have the values marked as true that I have checked in the View.
I have checked the Aquafina, Bisleri and Himalayan values (0, 5 and 6) that are true and the rest are false.
Output: Values retrieved at Controller [HttpPost] Action selector.
![]()
Conclusion
This completes the article on CheckboxFor in MVC4.