In this article I will explain with an example, how to populate (bind) DropDownList from database using
Entity Framework in ASP.Net Core Razor Pages.
Database
I have made use of the following table Customers 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 the following properties.
public class CustomerModel
{
[Key]
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_DropDownList_EF.Models;
namespace Razor_DropDownList_EF
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<CustomerModel> Customers { get; set; }
}
}
Razor PageModel (Code-Behind)
The PageModel consists of following 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.
Then, it is assigned to the public property of SelectList class Customers, which is used for populating DropDownList.
Handler method for handling POST operation
This Handler method handles the POST call when the Submit Button is clicked and the Form is submitted.
This Handler method accepts the
CustomerId and
Name of the selected Customer in an HTML SELECT element (DropDownList) as parameter which ultimately set into a
ViewData object.
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 += "\\nCustomer Id: " + customerId;
ViewData["Message"] = message;
}
}
Razor Page (HTML)
HTML Markup
The HTML of Razor Page consists of an HTML Form with an HTML SELECT element (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.
The Model data has been assigned to the DropDownList using the asp-items Tag Helpers attribute.
Inside the Razor Page, the following script file is inherited.
1. jquery.min.js
The DropDownList has been assigned with a
jQuery onchange event handler which is triggered when selected item of the DropDownList is changed.
Inside this event handler, the Hidden Field is set with the Name of the selected customer.
Form Submitting
When the
Submit Button is clicked, the
ViewData object checked for NULL and if it is not NULL then the
CustomerId and
Name values of the selected customer is displayed using
JavaScript Alert Message Box.
@page
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model Razor_DropDownList_EF.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">
<optionvalue="0">--Select Customer--</option>
</select>
<input type="hidden" name="CustomerName" />
<br />
<br />
<input type="submit" value="Submit" asp-page-handler="Submit" />
</form>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
$("body").on("change", "#ddlCustomers", function () {
$("input[name=CustomerName]").val($(this).find("option:selected").text());
});
</script>
@if (ViewData["Message"] != null)
{
<script type="text/javascript">
$(function () {
alert("@ViewData["Message"]");
});
</script>
}
</body>
</html>
Screenshot
Downloads