In this article I will explain with an example, how to populate (bind) DropDownList with
Entity Framework in ASP.Net Core MVC.
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
{
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 DropDownList_EF_Core
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<CustomerModel> Customers { get; set; }
}
}
Controller
The Controller consists of following 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 finally returned to the View.
Action method for handling POST operation
This Action 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.
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
ViewBag object.
Finally, the SelectList class object is returned to the View.
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, string customerName)
{
SelectList customers = new SelectList(this.Context.Customers.ToList(), "CustomerId", "Name");
ViewBag.Message = "Name: " + customerName;
ViewBag.Message += "\\nCustomer Id: " + customerId;
return View(customers);
}
}
View
HTML Markup
In the very first line, the SelectList class is declared as Model for the View.
The View consists of an HTML Form created 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 an HTML SELECT element (DropDownList), a Hidden Field and a Submit Button.
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
ViewBag 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.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@modelSelectList
@{
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>
<input type="hidden" name="CustomerName" />
<br />
<br />
<input type="submit" value="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 (ViewBag.Message != null)
{
<script type="text/javascript">
$(function () {
alert("@ViewBag.Message");
});
</script>
}
</body>
</html>
Screenshot
Downloads