In this article I will explain with an example, how to implement Searchable DropDownList using jQuery in ASP.Net Core Razor Pages.
The Searchable DropDownList will be populated from SQL Server database using Entity Framework in ASP.Net Core Razor Pages.
jQuery Plugin for Searchable DropDownList
This article makes use of Select2 jQuery Plugin for implementing Searchable DropDownList in ASP.Net Core Razor Pages.
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.
Namespaces
You will need to import the following namespace.
using Microsoft.AspNetCore.Mvc.Rendering;
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 Microsoft.EntityFrameworkCore;
using Razor_ComboBox_jQuery.Models;
namespace Razor_ComboBox_jQuery
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<Customer> Customers { get; set; }
}
}
Razor PageModel (Code-Behind)
The PageModel consists of two Action Handler methods.
Handler method for handling GET operation
Inside this Handler method, the records are fetched from the Customers table using Entity Framework and are copied to SelectList class object and assigned to the public property Customers, which is used for populating DropDownList in ASP.Net Core Razor Pages.
Handler method for handling Button Click and POST operation
This Handler method handles the POST call when the Submit Button is clicked and the Form is submitted.
When the Form is submitted, the Text and Value of the selected DropDownList Item are captured through the two parameters i.e. customerId and customerName.
The values of customerId and customerName are fetched and are set into a ViewData object which will be later displayed in View using JavaScript Alert Message Box.
public class IndexModel : PageModel
{
private DBCtx Context { get; }
public IndexModel(DBCtx _context)
{
this.Context = _context;
}
public SelectList Customers { get; set; }
public void OnGet()
{
this.Customers = new SelectList(this.Context.Customers, "CustomerId", "Name");
}
public void OnPostSubmit(string customerId, string customerName)
{
this.Customers = new SelectList(this.Context.Customers, "CustomerId", "Name");
string message = "Name: " + customerName;
message += "\\nID: " + customerId;
ViewData["Message"] = message;
}
}
Razor Page (HTML)
Form
The HTML of Razor Page consists of an HTML Form with a DropDownList, a Hidden Field and a Submit Button.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostSubmit but here it will be specified as Submit when calling from the Razor HTML Page.
DropDownList
The Model data has been assigned to the DropDownList using the asp-items Tag Helpers attribute.
The DropDownList has been assigned a jQuery OnChange event handler, when an item is selected in the DropDownList, the Text of the selected item is copied in the Hidden Field.
Form Submission
When the Submit Button is clicked, the Form gets submitted and the customerId and customerName values are sent to the Razor PageModel.
Finally, the ViewData object returned from the PageModel is checked for NULL and if it is not NULL then the customerId and customerName values of the selected Customer is displayed using JavaScript Alert MessageBox.
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.
@page
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model Razor_ComboBox_jQuery.Pages.IndexModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
<form method="post">
<select id="ddlCustomers" name="CustomerId" asp-items="@Model.Customers">
<option value="0">--Select Customer--</option>
</select>
<input type="hidden" name="CustomerName"/>
<br/>
<br/>
<input type="submit" value="Submit"asp-page-handler="Submit"/>
@if (ViewData["Message"] != null)
{
<script type="text/javascript">
window.onload = function () {
alert("@ViewData["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();
});
$("body").on("change", "#ddlCustomers", function () {
$("input[name=CustomerName]").val($(this).find("option:selected").text());
});
</script>
</form>
</body>
</html>
Screenshot
Downloads