top of page

.Net Core 5 DropDownList

In this article, we will see how to create a simple dropdown list in ASP.NET Core 5 MVC. Although, we know we have several ways to create a dropdown list, like using Html.DropdownlListFor helper or using the simple select tag using the jQuery. But whenever a framework update, it would be updated, with some new features and a few ones from the last update.

So as like this, here we learn the way to bind dropdown list in .NET Core 5.


Here, we bind an employee list in the dropdown. Yet, we are filling the employee dropdown with the static data. But you can fill this with your database. We are going to create the dropdown list like this,

We are mostly binding the dropdown list using the simple MVC HTML.DropdownListFor helper, it's good to fill the dropdown list. I am also using this method. But because of using MVC helpers, our ".cshtml page" becomes heavier. If any error occurred due to this, It would be directly redirected to the crashed page, which is not good for the client. It will leave a wrong impact on the client.

So, here we are creating the dropdown list using the select HTML helper, which is a lighter HTML element compare to the MVC HTML helper. .Net Core provides us the facility of using the "select tag's" asp-items property for directly binding the data with Text and Value.

So let's learn the new and simple way to bind dropdown using the Select HTML tag helper in .Net Core.

Step 1

First, create a simple ASP.NET Core web application(MVC) project define its name SimpleWebAppication. Select the target framework .Net 5.0 and then click on create.

Step 2

Now, we create an EmployeeViewModel.cs class. In this class, we will define the two properties EmployeeId, EmployeesList(contains the list of employees). As we know, that the SelectListItem gets the two values Text and Value property. So, like this, we will create our new class Item list that contains a similar text, Value properties.

In the constructor of EmployeeViewModel class, We will assign the EmployeesList property with the data of List<Itemlist>() type. EmployeeViewModel.cs class

public class EmployeeViewModel  
 public EmployeeViewModel()  
        EmployeesList = new List<Itemlist>() {  
 new Itemlist { Text = "Khushbu", Value = 1 },  
 new Itemlist { Text = "Mohan", Value = 2 },  
 new Itemlist { Text = "John", Value = 3 },  
 new Itemlist { Text = "Martin", Value = 4 },  
 new Itemlist { Text = "Due", Value = 5 },  
 public int EmployeeId { get; set; }  
 public List<Itemlist> EmployeesList { get; set; }  

Itemlist.cs class

public class Itemlist  
 public string Text { get; set; }  
 public int Value { get; set; }  

Step 3 Create an EmployeeDropdown action on the home controller and pass the EmployeeViewModel to the view.

public IActionResult EmployeeDropdown()  
    EmployeeViewModel model = new EmployeeViewModel();  
 return View(model);  

If you are getting data from the database, then you can bind it like below,

model.EmployeesList= data.Select(x => new Itemlist { Value = x.EmployeeId, Text = x.EmployeeName }).ToList();  

Step 4 Now, create EmployeeDropdown.cshtml page for displaying the employee dropdown list.

@model DotNet5DropDownList.Models.EmployeeViewModel  
    ViewData["Title"] = "Employee List";  
<div class="text-center">  
    <h1 class="display-4">Employee Dropdown</h1>  
    <form asp-action="EmployeeDropdown" asp-controller="home" method="post">  
        <div class="row">  
            <div class="col-md-4">  
                <select id="drpEmpList" class="form-control" asp-for="EmployeeId" asp-items="@(new SelectList(Model.EmployeesList, "Value", "Text"))">  
                    <option value="">--Select--</option>  
                <input type="hidden" asp-for="EmployeeId" />  
            <div class="col-md-2">  
                <input type="submit" name="btnSubmit" value="Submit" class="btn btn-success" />  

After submitting the dropdown, if we want to get the selected EmployeeId on the post method of the EmployeeDropdown action then, we can get it to value into the EmployeeId property of the EmployeeViewModel model. If you are not getting Id in property EmployeeId, then you forget to bind the property EmployeeId, into the hidden field over the page.

<input type="hidden" asp-for="EmployeeId" />  

 public IActionResult EmployeeDropdown(EmployeeViewModel model)  
           var EmployeeId = model.EmployeeId;  
 return RedirectToAction("EmployeeDropdown");  


In the above article, we will learn to create the simple .Net core 5 dropdown list using the select HTML helper and bind the data into the asp-items attribute so that the page becomes lighter and easy to manage.


The Tech Platform



bottom of page