Bind DropDownList With Enum In ASP.NET Core MVC

Enum datatype is used to define the named constants (i.e., the constant having a name and value). Enum can be used to populate a DropDownList control because Enum members have string names and an integer value associated with each one. See the below example with all steps.


Create the following Enum which we want to populate in the dropdown.
  1. public enum Country  
  2. {  
  3.    [Display(Name = "India")]  
  4.    India,  
  5.    [Display(Name = "United States of America")]  
  6.    usa,  
  7.    [Display(Name = "United Kingdom")]  
  8.    uk,  
  9.    [Display(Name = "Australia")]  
  10.    Australia  
  11. }  


In Model, we need to define a property of Enum.
  1. public class CountryModel  
  2. {  
  3.    public Country country { getset; }  
  4. }  


Create an action result for View.
  1. public IActionResult Country()  
  2. {  
  3.    return View();  
  4. }  


In View, call the model and include namespace where the County Enum is stored.
  1. @model Demo.Models.CountryModel  
  2. @using Demo.Models  
  3. @{  
  4.    ViewData["Title"] = "Country";  
  5. }  
Select Country
  1. <select asp-for="Country" asp-items="Html.GetEnumSelectList<Country>()" ></select>    
Hope this blog will help you in understanding how to bind a dropdownlist with Enum in ASP.NET Core MVC.
Your feedback is always welcome.