Monday, 24 January 2022

Asp.Net Core 6: How To Bind DropDownList From DataBase Using C#.Net

1/24/2022 - By Pranav Singh 0

In this article i will show you how you can bind DropDownList control in your asp.net core 6/mvc appliation from database data using c#.net. 


Other Articles: Asp.Net Core 6: Connect To Sql Server Database With Entity Framework Using C#Registration and Login Form Creation In Asp.net Core 6 Using C#.Net  From DatabaseAccess Connection String In Asp.Net Core 6 ClassLibrary From appsettings.json C#Asp.net Core 6: Ajax Login Form Using jQuery, C# Validate From Ms Sql Server Database


Now we will create a table named as employee and add the data in it.


Employee Table SQL

Now for this article fist we will create a new asp.net core 6 application. After creating asp.net core 6 web application we need to install below mention two packages from nuget package  manager. 

Microsoft.EntityFrameworkCore.SqlServer

Microsoft.EntityFrameworkCore.Tools

Now we need to run the below mention command to create the DB context. 

Scaffold-DbContext "Server=..\SQLEXPRESS;Database=TestDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir data


After this we will get all the entity table as a class file in mention data folder. 


entity table

Now we will create controller class file and add the below mention HTTPGET method. 


[HttpGet]

        public IActionResult Index()

        {

            return View();

        }


Now we will create the model class file and add the below mention code.


using Microsoft.AspNetCore.Mvc.Rendering;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

 

namespace WebApplication1.Models

{

    public class EmployeeModel

    {

        public List<SelectListItem> EmployeeList { get; set; }

    }

}


In above code i have take a selectlistitem of list type and give it name employeelist. Now we will create a view .cshtml file and add the below code in it.


@model WebApplication1.Models.EmployeeModel

@{

    ViewData["Title"] = "Home Page";

}

 

<div class="text-center">

    @Html.DropDownListFor(m => m.EmployeeId, new SelectList(Model.EmployeeList, "value", "text"))

</div>

 


 Now we will come to our controller and add the below code into it.


using Microsoft.AspNetCore.Mvc;

using Microsoft.AspNetCore.Mvc.Rendering;

using Microsoft.Extensions.Logging;

using System;

using System.Collections.Generic;

using WebApplication1.data;

using WebApplication1.Models; 

namespace WebApplication1.Controllers

{

    public class HomeController : Controller

    {

        private readonly ILogger<HomeController> _logger;

 

        public HomeController(ILogger<HomeController> logger)

        {

            _logger = logger;

        }

        [HttpGet]

        public IActionResult Index()

        {

            EmployeeModel employeeModel = new EmployeeModel();

            employeeModel.EmployeeList = new List<SelectListItem>();

            employeeModel.EmployeeList.Add(new SelectListItem

            {

                Value = "",

                Text = "Select Employee"

            });

 

            TestDBContext testDBContext = new TestDBContext();

            var data = testDBContext.Employees;

            foreach (var item in data)

            {

                employeeModel.EmployeeList.Add(new SelectListItem

                {

                    Value = Convert.ToString(item.Id),

                    Text = item.EmployeeName

                });

            }

            return View(employeeModel);

        }

    }

}


In above code i have created the object of the model class and the created the entity of the DBContext. After getting the employee list and added to the employee model selectlist. Now we have done. Run the code and check the output.

employee dropdown in asp.net core
Note: You must change the connection string in DB context file. 

Download

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top