Bind Dropdownlist in ASP.NET Core

Problem Defination :- We have a requirement in which we need to bind the table data into Dropdownlist in ASP .Net Core application. For example we have a Country master table and we need to present these countries through the dropdownlist in UI.

Below is the step by step process explain in details so that you can follow it and bind a dropdownlist in your application.

Step 1 :- Create Project using Microsoft Visual Studio

Visual Studio start window with Create a new project selected

Select Project Type ASP.Net Core Web App(Model-View-Controller)

Selecting the ASP.NET Core Web App (Model-View-Controller) template in Visual Studio

Write Name of Your Application Project

Entering the project name in the Configure your new project dialog

Choose Suitable framework version and Check the checkbox for Configure Https

Choosing the .NET framework version and Configure for HTTPS option

Your Solution explorer looks like as in image

Solution Explorer of the new ASP.NET Core MVC project

Step 2 :- Install the required packages from nuget package manager

Opening NuGet Package Manager to install the required packages

Select Microsoft.EntityFrameworkCore.SqlServer with compatible version and install it.

Installing the Microsoft.EntityFrameworkCore.SqlServer NuGet package

Select Microsoft.EntityFrameworkCore.Tools with compatible version and install it.

Installing the Microsoft.EntityFrameworkCore.Tools NuGet package

Select Microsoft.EntityFrameworkCore with compatible version and install it.

Installing the Microsoft.EntityFrameworkCore NuGet package

You can check installed packages as shown in below image

Installed Entity Framework Core packages under Dependencies in Solution Explorer

Step 3 :- Create a Model Class Country for dropdownlist binding from database.

                
 public class Country
    {
        [Key]
        public int CountryId { get; set; }
        public string CountryName { get; set; }
    }  

Step 4 :- Create a AppDbContext class for Dbcontext and use Country model class as a DBSet as shown in below image and code.

Country model class with CountryId and CountryName properties
                
 public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions options) : base(options)
        {

        }
        public DbSet tblCountries { get; set; }
    }  

Step 5 :- Define a Connection string into Appsettings.Json file as shown in below image.

SQL Server connection string defined in appsettings.json

Step 6 :- Add a Middileware for DBContext in Program.cs file as shown in below image.

Registering AppDbContext with the SQL Server connection string in Program.cs

Step 7 :- Trigger the Add-Migration "FirstMigration" Command as shown in below image.

Running Add-Migration FirstMigration in Package Manager Console

Step 8 :- Trigger the Update-Database Command as shown in below image.

Running Update-Database in Package Manager Console

After triggering above migration commands you can see a newly generated migration folder in solution and there is a FirstMigration Class is autogenerated from migration as shown in below image.

Migrations folder with the auto-generated FirstMigration class

After triggering above migration commands you can see a newly generated migration tables in databse and there are some other migration history tables are also autogenerated from migration as shown in below image.

Tables created in SQL Server by the Entity Framework Core migration

Step 9 :- Create a Controller and Named it TestController and inside controller create a action Method named it Index as shown in below code.

                
  public class TestController : Controller
    {
        private readonly AppDbContext db;
        public TestController(AppDbContext _db)
        {
            db = _db;
        }

        public IActionResult Index()
        {
            var dropdownItems = db.tblCountries.ToList();
            ViewBag.Country = new SelectList(dropdownItems, "CountryId", "CountryName");
            return View();
        }
    } 
TestController binding countries to ViewBag with a SelectList

Step 10 :- Create a View and write the below Html code into it as shown in image.

Razor view code that renders the country dropdown list

Now run the application you will get the desired output as shown in below image.

Country dropdown list bound from the database in ASP.NET Core MVC