In this article I will explain with an example, how to display multiple CheckBoxes with Checked Unchecked values in ASP.Net MVC Razor.
The CheckBoxes along with their checked or unchecked (selected) values will be populated from SQL Server database using Entity Framework in ASP.Net MVC Razor.
I have made use of the following table Hobbies with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Creating an Entity Data Model
You will need to import the following namespace.
The Controller consists of the following Action method.
Action method for handling GET operation
Inside this Action method, first the records from the database are fetched using Entity Framework.
Finally, the Generic list of Hobbies is returned to the View.
public class HomeController : Controller
// GET: Home
public ActionResult Index()
using (HobbiesEntities entities = new HobbiesEntities())
Inside the View, in the very first line the Generic list of Hobby class is declared as Model for the View.
Then a loop is executed over the Generic list of Hobby class objects and an HTML Table is generated consisting of a CheckBox and a Label.
The CheckBox will be checked or unchecked based on the value of the IsSelected property.
Layout = null;
<meta name="viewport" content="width=device-width"/>
<table border="0" cellpadding="0" cellspacing="0">
@foreach (Hobby hobby in Model)
@Html.CheckBoxFor(m => hobby.IsSelected)
@Html.DisplayFor(m => hobby.Description)