In this article I will explain with an example, how to populate (bind) DropDownList from database using Entity Framework in ASP.Net Core MVC.
This article will also explain how to add Default Item (Blank Item) to DropDownList at First position when populating from database using Entity Framework 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.
ASP.Net Core: DropDownList with Entity Framework Tutorial with example
 
I have already inserted few records in the table.
ASP.Net Core: DropDownList with Entity Framework Tutorial with example
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
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.
Note: For beginners in ASP.Net Core and Entity Framework, please refer my ASP.Net Core: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core.
 
using DropDownList_EF_Core.Models;
using Microsoft.EntityFrameworkCore;
 
namespace DropDownList_EF_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 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 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 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, string customerName)
    {
        ViewBag.Message = "Name: " + customerName;
        ViewBag.Message += "\\nID: " + customerId;
        return View();
    }
}
 
 
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 Form consists of a DropDownList, a Hidden Field and a Submit Button.
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.
When the Submit Button is clicked, the Form gets submitted and the CustomerId and CustomerName values are sent to the Controller.
Finally, the CustomerId and CustomerName values of the selected Customer are displayed using JavaScript Alert Message Box.
@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>
        <input type="hidden" name="CustomerName"/>
        <br/>
        <br/>
        <input type="submit" value="Submit"/>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/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>
        }
    </form>
</body>
</html>
 
 
Screenshot
ASP.Net Core: DropDownList with Entity Framework Tutorial with example
 
 
Downloads