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 Database, Access 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.
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.
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> |
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.
Note: You must change the connection string in DB context file.Download
0 comments:
Please let me know your view