In this article I will explain with an example, how to implement Searchable DropDownList in ASP.Net Core MVC.
The Searchable DropDownList will be populated from SQL Server database using Entity Framework in ASP.Net Core MVC.
jQuery Plugin for Searchable DropDownList
This article makes use of Select2 jQuery Plugin for implementing Searchable DropDownList in ASP.Net Core MVC.
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
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 the following two properties.
Note: In this article, only two Columns will be used and hence two properties are added to the class.
public class Customer
{
public int CustomerId { get; set; }
public string Name { 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 ComboBox_jQuery_MVC_Core.Models;
using Microsoft.EntityFrameworkCore;
namespace ComboBox_jQuery_MVC_Core
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<Customer> Customers { get; set; }
}
}
Controller
The Controller consists of the following two Action methods.
Action method for handling GET operation
Inside this Action method, the records are fetched from the Customers table using Entity Framework and are copied to SelectList class object which is used for populating DropDownList in .Net Core.
Finally, the SelectList class object is sent 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 Value of the selected DropDownList Item is captured through the parameter customerId.
Then the selected Customer is fetched using Lambda expression.
Finally, the Name and ID values of the selected Customer 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()
{
SelectList customers = new SelectList(this.Context.Customers.ToList(), "CustomerId", "Name");
return View(customers);
}
[HttpPost]
public IActionResult Index(string customerId)
{
SelectList customers = new SelectList(this.Context.Customers.ToList(), "CustomerId", "Name");
SelectListItem selectedItem = customers.ToList().Find(p => p.Value == customerId);
ViewBag.Message = "Name: " + selectedItem.Text;
ViewBag.Message += "\\nID: " + selectedItem.Value;
return View(customers);
}
}
View
Inside the View, in the very first line SelectList class is declared as Model for the View.
Form
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 Form consists of a DropDownList, a Submit Button.
DropDownList
The Model data has been assigned to the DropDownList using the asp-items Tag Helpers attribute.
Form Submission
When the Submit Button is clicked, the Form gets submitted and the CustomerId value is sent to the Controller.
Finally, the Name and ID values of the selected Customer are displayed using JavaScript Alert Message Box.
Select2 jQuery Plugin implementation
First, the jQuery and the JS and CSS files of the jQuery Select2 plugin are inherited.
Then inside the jQuery document.ready event handler, the jQuery Select2 plugin is applied to the DropDownList.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model SelectList
@{
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">
<select id="ddlCustomers" name="CustomerId" asp-items="Model">
<option value="0">--Select Customer--</option>
</select>
<br/>
<br/>
<input type="submit" value="Submit"/>
@if (ViewBag.Message != null)
{
<script type="text/javascript">
window.onload = function () {
alert("@ViewBag.Message");
};
</script>
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css"/>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js"></script>
<script type="text/javascript">
$(function () {
$("#ddlCustomers").select2();
});
</script>
</form>
</body>
</html>
Screenshot
Downloads