Introduction
In this article I will explain how to populate a dropdown list from database values in ASP.NET Core MVC applications. An MVC application consists of model, view and controller. In this application I will create and scaffold a model to work with the database and retrieve data from SQL Server database. The controller will be responsible for selecting a view to be displayed to the user and provide necessary data model to retrieve and display data into the dropdown list from SQL Server database. The controller manages how the application would respond to the get request made to SQL Server database.
Creating ASP.NET Core MVC web applications
Before writing the coding part of ASP.NET Core MVC application, first we need to create a table in SQL Server database so that values available in the database can be retrieved and bind to dropdown list in the ASP.NET Core MVC web application.
The table can be created with the help of the following SQL query.
- create table dbo.country
- (
- Cid int not null identity(1,1) primary key,
- Cname varchar(100)
- );
The database table looks like the following

Now we will create a new ASP.NET Core MVC application as follows.


Here we will select ASP.NET Core Web application, provide a project name and select model-view-controller as web application template.
Now we will be adding a connection string inside appsettings.json to connect to SQL Server database.
- {
- "ConnectionStrings": {
- "Myconnection": "Data Source=DESKTOP-QGAEHNO\\SQLEXPRESS;Initial Catalog=profiledb;Integrated Security=True"
- },
- "Logging": {
- "LogLevel": {
- "Default": "Information",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
- },
- "AllowedHosts": "*"
- }
The next step is to add a class named Country inside models folder. Inside Country class, we will be defining two properties, Cid and Cname, to retrieve and bind database country column values to dropdown list controls.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.ComponentModel.DataAnnotations;
- namespace BindingData.Models
- {
- public class Country
- {
- [Key]
- public int Cid { get; set; }
- public string Cname { get; set; }
- }
- }
Here Cid is a primary key which is of integer type and Cname is of type string.


Jet WellPosted Aug 6, 2023, 9:43 PM
Thanks , Dear Abhishek Yadav it's very simple and productive method
Kenneth BeaudryPosted Nov 6, 2022, 2:15 PM
Nice and clear and it helped me.