In this article I will explain with an example, how to populate (bind) CheckBoxList from database using Entity Framework in ASP.Net MVC Razor.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
 
 
Database
I have made use of the following table Hobbies with the schema as follows.
Populate (Bind) CheckBoxList from Database using Entity Framework in ASP.Net MVC
 
I have already inserted few records in the table.
Populate (Bind) CheckBoxList from Database using Entity Framework in ASP.Net MVC
 
Note: You can download the database table SQL by clicking the download link below.
        Download SQL file
 
 
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Populate (Bind) CheckBoxList from Database using Entity Framework in ASP.Net MVC
 
 
Controller
The Controller consists of following two Action methods.
Action method for handling GET operation
Inside this Action method, the records from the Hobbies table are fetched using Entity Framework and are copied into generic list of SelectListItem class.
Note: The SelectListItem class which is an in-built ASP.Net MVC class. It has all the properties needed for populating a CheckBoxList.
 
The HobbyName is stored in the Text property while the HobbyId is stored in the Value property.
Finally, the generic list of SelectListItem class is returned to the View.
 
Action method for handling POST operation
This Action method handles the call made from the POST function from the View.
When the Form is submitted, the posted values are captured through the SelectListItem class object as parameter.
Finally, a loop is executed over the generic list of SelectListItem class objects and the selected Hobby names are set into a ViewBag object which will be later displayed in View using JavaScript Alert Message Box.
Note: For more details on displaying message using JavaScript Alert MessageBox, please refer my article ASP.Net MVC: Display Message from Controller in View using JavaScript Alert MessageBox.
 
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        using (HobbiesEntities entities = new HobbiesEntities())
        {
            List<SelectListItem> hobbiesList = (from p in entities.Hobbies.AsEnumerable()
                                                select new SelectListItem
                                                {
                                                    Text = p.HobbyName,
                                                    Value = p.HobbyId.ToString()
                                                }).ToList();
            return View(hobbiesList);
        }
    }
 
    [HttpPost]
    public ActionResult Index(List<SelectListItem> items)
    {
        ViewBag.Message = "Selected Items:\\n";
        foreach (SelectListItem item in items)
        {
            if (item.Selected)
            {
                ViewBag.Message += string.Format("{0}\\n", item.Text);
            }
        }
 
        return View(items);
    }
}
 
 
View
Inside the View, in the very first line Generic list of SelectListItem class is declared as Model for the View.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
 
Inside the Form, a loop is executed over the generic list of SelectListItem class objects and an HTML Table is generated consisting of a CheckBoxList, a Label and two HiddenField elements generated using the following HTML helper methods.
Html.CheckBoxFor – Creating CheckBox for the Model property.
Html.DisplayFor – Displaying the text for each CheckBox.
Html.HiddenFor – Create HTML hidden input element for the Model property.
Note: The HiddenFields are used for storing the Text and Value of the CheckBox for sending it back during Form Submission.
 
Also there is a Submit Button which when clicked, the Form gets submitted and the Model object is sent to the Controller.
Finally, the selected Hobby names are displayed using JavaScript Alert Message Box.
@model List<SelectListItem>
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <table>
            @for (int i = 0; i < Model.Count(); i++)
            {
                <tr>
                    <td>
                        @Html.CheckBoxFor(m => m[i].Selected)
                    </td>
                    <td>
                        @Html.DisplayFor(m => m[i].Text)
                        @Html.HiddenFor(m => m[i].Value)
                        @Html.HiddenFor(m => m[i].Text)
                    </td>
                </tr>
            }
        </table>
        <br />
        <input type="submit" value="Submit" />
    }
    @if (ViewBag.Message != null)
    {
        <script type="text/javascript">
            window.onload = function () {
                alert("@ViewBag.Message");
            };
        </script>
    }
</body>
</html>
 
 
Screenshot
Populate (Bind) CheckBoxList from Database using Entity Framework in ASP.Net MVC
 
 
Downloads