In this article I will explain with an example, how to bind (populate)
Multiple Select (Multi-Select)
DropDownList with
CheckBoxes from database using
jQuery in
ASP.Net Core (.Net Core 8)
MVC.
The Multiple Select (Multi-Select)
DropDownList with
CheckBoxes will be implemented using
jQuery Bootstrap Multi-Select plugin.
Download Bootstrap and jQuery Bootstrap Multi-Select Plugin
The download locations are as follows.
Bootstrap
jQuery BoosStrap Multi-Select Plugin
You will need to download the plugin files from the following location.
The complete documentation can be read on the following page.
Database
I have made use of the following table Fruits 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.
Model
The Model class consists of following properties.
The property
FruitId 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 FruitModel
{
[Key]
public int FruitId { get; set; }
public string FruitName { get; set; }
}
Database Context
Once the
Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
using Microsoft.EntityFrameworkCore;
namespace CheckBox_DropDownList_MVC_Core
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<FruitModel> Fruits { 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 Action methods.
Action method for handling GET operation
Inside this Action method, the PopulateFruits method is called.
Inside the
PopulateFruits method, the records from the
Fruits 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 FruitId of selected Fruits are captured in the FrutIds parameter.
Then, a loop is executed over the FrutIds and the selected Fruit is fetched using Lambda expression.
Finally, the selected
Fruit names are set into a
ViewBag object which will be later displayed in View using
JavaScript Alert Message Box.
public class HomeController : Controller
{
private DBCtx Context { get; }
public HomeController(DBCtx _context)
{
this.Context = _context;
}
public IActionResult Index()
{
return View(this.PopulateFruits());
}
[HttpPost]
public IActionResult Index(string[]fruitIds)
{
List<SelectListItem> fruits = this.PopulateFruits();
if (fruitIds != null)
{
ViewBag.Message = "Selected Fruits:";
foreach (string fruitId in fruitIds)
{
SelectListItem selectedFruit = fruits.Find(p => p.Value == fruitId);
selectedFruit.Selected = true;
ViewBag.Message += "\\n" + selectedFruit.Text;
}
}
return View(fruits);
}
private List<SelectListItem> PopulateFruits()
{
List<SelectListItem>fruitsList = (from p in this.Context.Fruits
select new SelectListItem
{
Text = p.FruitName,
Value = p.FruitId.ToString()
}).ToList();
return fruitsList;
}
}
View
HTML Markup
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.
The
Model data has been assigned to the
HTML Select (
DropDownList) element using the
asp-items Tag Helpers attribute.
The
multiple attribute of the
HTML Select (
DropDownList) element is set which enables multiple selections.
Bootstrap Multiselect jQuery Plugin implementation
First, the
jQuery and the
JS and
CSS files of the
Bootstrap plugin and the
jQuery Bootstrap Multi-Select plugin are inherited.
Inside the
jQuery document ready event handler, the
jQuery Bootstrap Multi-Select plugin is applied to the
HTML Select (
DropDownList) element.
Form Submission
When the Submit Button is clicked, the Form gets submitted and the values of the selected Fruits are sent to the Controller.
Finally, the multiple selected
Fruit 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">
Fruits:
<br /><br />
<select name="FruitIds" asp-items="Model" multiple="multiple" class="listbox"></select>
<br /><br />
<input type="submit" value="Submit" />
</form>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('.listbox').multiselect({
includeSelectAllOption: true
});
});
</script>
@if (ViewBag.Message != null)
{
<script type="text/javascript">
window.onload = function () {
alert("@ViewBag.Message");
};
</script>
}
</body>
</html>
Screenshot
Downloads