---
title: "how to bind dropdownlist in mvc core from database using entity framework"  
description: "In this article we will learn how to bind DropDownList from database in asp.net core MVC razor page with Entity framework."  
author: "Anonymous User"  
published: 2020-11-27  
updated: 2020-12-01  
canonical: https://www.mindstick.com/articles/324837/how-to-bind-dropdownlist-in-mvc-core-from-database-using-entity-framework  
category: "asp.net core"  
tags: ["mvc"]  
reading_time: 3 minutes  

---

# how to bind dropdownlist in mvc core from database using entity framework

## [Introduction](https://www.mindstick.com/blog/359/iphone-development-introduction):

In this article we will learn how to bind [DropDownList](https://www.mindstick.com/blog/33/dropdownlist-in-asp-dot-net) from [database in asp.net](https://answers.mindstick.com/qa/94215/how-to-create-xml-file-automatically-when-any-new-record-is-inserted-in-database-in-asp-dot-net-mvc-also-delete-and-update-that-xml-file-base-on-record) [core MVC](https://www.mindstick.com/forum/158980/why-dot-net-core-mvc-page-not-refreshing) razor page with [Entity framework](https://www.mindstick.com/forum/12875/how-to-use-object-query-with-a-where-and-to-retrieve-entity-record-entity-framework).

## Description:

Now here we are going to bind DropDownList from database in [ASP.NET Core](https://www.mindstick.com/forum/160440/describe-the-advancements-in-asp-dot-net-core-in-dot-net-core-6) MVC, using Entity [Framework Core](https://www.mindstick.com/forum/159842/how-to-read-record-from-database-in-asp-mvc-core-using-entity-framework-core-1-0) with example.

Now let’s create a demo to bind DropDownList from database in asp.net core MVC.

**[Create table](https://www.mindstick.com/articles/443/how-to-create-table-in-sql-server) following below script:**

```
CREATE TABLE country
(
  ID BIGINT IDENTITY(1,1) PRIMARY KEY,
  Name VARCHAR(150)
)
```

**[Insert data](https://www.mindstick.com/forum/103/how-to-insert-data-in-identity-column) in table following below script:**

```
USE [collage]
GO
SET IDENTITY_INSERT [dbo].[country] ON
INSERT [dbo].[country] ([ID], [Name]) VALUES (1, N'India')
INSERT [dbo].[country] ([ID], [Name]) VALUES (2, N'Afghanistan')
INSERT [dbo].[country] ([ID], [Name]) VALUES (3, N'Australia')
INSERT [dbo].[country] ([ID], [Name]) VALUES (4, N'Germany')
INSERT [dbo].[country] ([ID], [Name]) VALUES (5, N'Iran')
INSERT [dbo].[country] ([ID], [Name]) VALUES (6, N'Iraq')
INSERT [dbo].[country] ([ID], [Name]) VALUES (7, N'Japan')
INSERT [dbo].[country] ([ID], [Name]) VALUES (8, N'Lesotho')
INSERT [dbo].[country] ([ID], [Name]) VALUES (9, N'Lithuania')
INSERT [dbo].[country] ([ID], [Name]) VA
```

**Now write following code in index.cshtml.**

```
@model AspdotNetCoreMvc.Models.Student
@{
   ViewData["Title"] = "Home Page";
}
 <div class="top-buffer"></div>
<div class="col-md-4">
   <form asp-controller="Home" asp-action="Index" method="post">
       <div class="row">
            <div class="col-md-12">
                <label asp-for="Name"></label>
                <input asp-for="Name" class="form-control" />
            </div>
       </div>
       <div class="row">
            <div class="col-md-12">
                <label asp-for="Address"></label>
                <input asp-for="Address" class="form-control" />
            </div>
       </div>
       <div class="row">
            <div class="col-md-12">
                <label asp-for="Country"></label>
                <select asp-for="Country"  class="form-control"
                        asp-items="@(new SelectList(@ViewBag.countries,"Id", "Name"))"></select>
            </div>
       </div>

       <br /><button type="submit">Register</button>

</form>
</div>
```

**Now write the following code in HomeController.**

```
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using AspdotNetCoreMvc.Models;

namespace AspdotNetCoreMvc.Controllers
{
    public class HomeController : Controller
    {
        collageContext context = new collageContext();
        public IActionResult Index()
        {
            ViewBag.countries =context.Country.ToList();
            Student student = new Student();
             return View(student);
        }
        [HttpPost]
        public IActionResult Index(Student student)
        {
            ViewBag.countries =context.Country.ToList();

            return View(student);
        }
    }
}
```

**Now build and run the [application](https://www.mindstick.com/blog/59/xaml-extensible-application-markup-language) using CTRL + 5. It should bring up the following page.**

![how to bind dropdownlist in mvc core from database using entity framework](https://www.mindstick.com/mindstickarticle/663f183d-ad62-4471-ad7c-7eec59332c67/images/f704b734-a0a0-4e4d-b79b-be368a99a4eb.png) **\**

I hope it will help to you.

---

Original Source: https://www.mindstick.com/articles/324837/how-to-bind-dropdownlist-in-mvc-core-from-database-using-entity-framework

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
