In this article I will explain with an example, how to populate (bind) CheckBoxList from database using Entity Framework in ASP.Net Core MVC.
 
 
Database
I have made use of the following table Hobbies with the schema as follows.
ASP.Net Core MVC: Populate (Bind) CheckBoxList from Database using Entity Framework
 
I have already inserted few records in the table.
ASP.Net Core MVC: Populate (Bind) CheckBoxList from Database using Entity Framework
 
Note: You can download the database table SQL by clicking the download link below.
        Download SQL file
 
 
Model
The Model class consists of the following three properties.
The property HobbyId is decorated with the following Data Annotation attributes:
Key – It is used to apply the Primary Key constraint to the Column.
using System.ComponentModel.DataAnnotations;
 
public class HobbyModel
{
    [Key]
    public int HobbyId { get; set; }
    public string Hobby { get; set; }
    public bool IsSelected { get; set; }
}
 
 
Database Context
Once the Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
Note: For beginners in ASP.Net Core and Entity Framework, please refer my article ASP.Net Core: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core.
 
using Microsoft.EntityFrameworkCore;
 
namespace CheckBoxList_EF_MVC_Core
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
 
        public DbSet<HobbyModel> Hobbies { get; set; }
    }
}
 
 
Namespaces
You will need to import the following namespaces.
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Rendering;
 
 
Controller
The Controller consists of following two Action methods.
Action method for handling GET operation
Inside this Action method, the PopulateHobbies method is called.
Inside the PopulateHobbies method, the records from the Hobbies table are fetched using Entity Framework and are copied into generic list of SelectListItem class and 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 HobbyId of selected Hobby are captured in the Hobby 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 Core: Display Message from Controller in View using JavaScript Alert MessageBox.
 
public class HomeController : Controller
{
    private DBCtx Context { get; }
    public HomeController(DBCtx _context)
    {
        this.Context = _context;
    }
 
    public IActionResult Index()
    {
        return View(this.PopulateHobbies());
    }
 
    [HttpPost]
    public IActionResult Index(string[] hobby)
    {
        ViewBag.Message = "Selected Items:\\n";
        List<SelectListItem> items = this.PopulateHobbies();
        foreach (SelectListItem item in items)
        {
            if (hobby.Contains(item.Value))
            {
                item.Selected = true;
                ViewBag.Message += string.Format("{0}\\n", item.Text);
            }
        }
 
        return View(items);
    }
 
    private List<SelectListItem> PopulateHobbies()
    {
        List<SelectListItem> hobbiesList = (from p in this.Context.Hobbies
                                            select new SelectListItem
                                            {
                                                Text = p.Hobby,
                                                Value = p.HobbyId.ToString()
                                            }).ToList();
 
        return hobbiesList;
    }
}
 
 
View
Inside the View, in the very first line ASP.Net TagHelpers is inherited and Generic list of SelectListItem class is declared as Model for the View.
The View consists of an HTML Form with following ASP.Net Tag Helpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
 
Inside the HTML Form, a loop is executed over the generic list of SelectListItem class objects and an HTML Table is generated consisting of a group of HTML Label elements and HTML CheckBoxes.
Also there is a Submit Button which when clicked, the Form gets submitted and the values of the selected CheckBoxes are sent to the Controller.
Finally, the selected Hobby names are displayed using JavaScript Alert Message Box.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model List<SelectListItem>
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post" asp-controller="Home" asp-action="Index">
        <table>
            @foreach (var hobby in Model)
            {
                <tr>
                    <td>
                        <input id="@hobby.Value" type="checkbox" name="Hobby" value="@hobby.Value" checked="@hobby.Selected" />
                    </td>
                    <td>
                        <label for="@hobby.Value">@hobby.Text</label>
                    </td>
                </tr>
            }
        </table>
        <br />
        <input type="submit" value="Submit" />
    </form>
    @if (ViewBag.Message != null)
    {
        <script type="text/javascript">
            window.onload = function () {
                alert("@ViewBag.Message");
            };
        </script>
    }
</body>
</html>
 
 
Screenshot
ASP.Net Core MVC: Populate (Bind) CheckBoxList from Database using Entity Framework
 
 
Downloads