how to bind dropdownlist in mvc core from database using entity framework

how to bind dropdownlist in mvc core from database using entity framework

Introduction:

In this article we will learn how to bind DropDownList from database in asp.net core MVC razor page with Entity framework. 

Description:

Now here we are going to bind DropDownList from database in ASP.NET Core MVC, using Entity Framework Core with example.  

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

Create table following below script:

CREATE TABLE country

(
  ID BIGINT IDENTITY(1,1) PRIMARY KEY,
  Name VARCHAR(150)
)

Insert data 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 using CTRL + 5. It should bring up the following page.

how to bind dropdownlist in mvc core from database using entity framework

I hope it will help to you.

Last updated:12/1/2020 2:49:54 AM
Aditya Patel

Aditya Patel

Hi, I am a Senior Software Developer in MindStick Software pvt. Ltd. I have done BCA from dr. c.v. Raman university. I have more than five years experience in Microsoft Technology MVC 4, MVC Core, C#,Vb.Net, rdlc Reporting ,SQL Server and other jQuery, css, Bootstrap, Knockout.js, angular.js,react.js Html, Crystal Reporting etc.

0 Comments

Leave Comment