Introduction
In this article, I will explain how to create a multi-checkbox dropdown list in ASP.NET Core 3.1 MVC. I am going to create a multi-select checkbox dropdown list with the help of "bootstrap-multiselect" CSS and JS. Also, we will see how to edit this dropdown list with database data. Along with this, I'll show you how you maintain a database for this type of data and display a list of data.
Overview
Here, I am going to create a teacher example, where I create a teacher with their name and multiple subjects and save them into the database, display the teacher list and edit each teacher with their subject list.
We are creating a dropdown list that looks like this:
![Multi Select Checkbox Dropdown List Create And Edit In .Net Core 3.1]()
Let's see how to create this:
Step 1
Create a .NET Core 3.1 web application
Step 2
Prepare the database for this holding this type of data, create 3 tables.
Table 1 - Teacher( holds teacher information)
Table 2-Subjects (holds all subjects)
Table 3-TeacherSubjects (holds teacher selected subject)
![Multi Select Checkbox Dropdown List Create And Edit In .Net Core 3.1]()
![Multi Select Checkbox Dropdown List Create And Edit In .Net Core 3.1]()
![Multi Select Checkbox Dropdown List Create And Edit In .Net Core 3.1]()
Step 3
Embed the following CSS and JS into your project:
- bootstrap-multiselect.css
- bootstrap-multiselect.js
![Multi Select Checkbox Dropdown List Create And Edit In .Net Core 3.1]()
![Multi Select Checkbox Dropdown List Create And Edit In .Net Core 3.1]()
Step 4
Create a MainController.cs in which creates an index method for listing all teachers:
Step 5
Now create an index.cshtml view for displaying all teachers list. Here, I create an additional link button for adding a teacher and render an edit link with each teacher.
index.cshtml view
Step 6
Now let's create a method for adding teacher into the database
TeacherDto.cs
Above, we first find the Id's teacher, and because we have a foreign key relationship with TeacherSubjects Table, for accessing that teacher's Subjects list we have to include that table using LINQ Include ("TeacherSubjects") method. Then add each subject id into the long type List name subjectsIds, and pass this subjectsIds into the model by casting it with ToArray().
Step 7
Now create a HttpPost method for submitting and updating teachers with the database.
Step 8
Here is the complete MainController.cs:
Step 9
Create a AddTeacher.cshtml view:
![Multi Select Checkbox Dropdown List Create And Edit In .Net Core 3.1]()
![Multi Select Checkbox Dropdown List Create And Edit In .Net Core 3.1]()
The "multiselect" is the method of bootstrap-multiselect.js which creates our dropdown list
Output
On the index page, we are displaying all teacher's lists.
![Multi Select Checkbox Dropdown List Create And Edit In .Net Core 3.1]()
Here is the Add Teacher view for adding a teacher:
![Multi Select Checkbox Dropdown List Create And Edit In .Net Core 3.1]()
And the Edit teacher View:
![Multi Select Checkbox Dropdown List Create And Edit In .Net Core 3.1]()
Summary
In the above article, we learn to create and edit a multi-select checkbox dropdown list in ASP.NET Core 3.1 MVC, and adding a boostrap-multiselect CSS and JS for displaying the layout of this dropdown.