Creating a Drop Down menu using Enum in ASP.net core
Also known as enumeration data type, Enum is a value type. It defines a set of constants that is assigned to a variable. The set of values declared inside the enum is known as the enumeration list. The enum is usually defined as public and can be accessed and used over all the classes within the namespace.
The enum is best to define directly within a namespace so that all the classes in the namespace can access it. The first enumerator by default has the value 0 and the value of each enumerator after that is increased by one.
A simple example of an enum:
enum Colors
{
Red,
Blue,
Orange,
Green
}
Other example of enums commonly used for:
- Day {Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday}
- Rectangle{Left,Top,Bottom,Right}
- WeekEnd{Jan,Feb,Mar,Apr,May,Jun,Aug,Seg,Oct,Nov,Sunday}
- Point {X,Y}
Now How to create a dropdown menu using enum in ASP.net:
Firstly we define a model where we provide the definition and declaration for the enumeration list and the member SelectionField of enum type for taking the selected value from the enumeration list:
Another member CountryName is for Displaying the selected country name:
namespace ViewExample.Models
{
Public class Employee
{
public string CountryName { get; set; }
public Selection SelectionField { set; get; }
public enum Selection
{
NotSelected,
India,
Nepal,
China,
Bhutan,
Maldives
}
}
Now, We add a controller action to the home controller which returns the required view for displaying the dropdown menu.
[HttpGet]
public IActionResult EmployeeValidation()
{
Employee emp = new Employee();
return View(emp);
}
[HttpPost]
public IActionResult EditEmployee(Employee employee)
{
return View(employee);
}
Now we also need to add a view inside the Home folder in Views i.e the same name as the action name in the controller i.e EmployeeValidation in our case:
@model Employee;
@{
ViewData["Title"] = "EmployeeValidation";
}
<form action="/Home/EditEmployee" method="post">
<div class="form-group">
<label asp-for="CountryName">Select Country:</label><br />
<select name="selectionField" asp-items="Html.GetEnumSelectList<Selection>()"></select><br />
<span asp-validation-for="SelectionField" class="text-danger" />
</div><br />
</form>
The “Html.GetEnumSelectList<Selection>()” shows the items from the enumeration list in the dropdown list.
Furhermore we also need to add the controller and action name to the main _Layout.cshtml file in shared folder as:
<li><a asp-area="" asp-controller="Home" asp-action="EmployeeValidation">Validation</a></li
Now we can also display the selected field from the dropdown menu to another View inside the Home folder by creating a new View and providing the selected value from the dropdown menu as:
Here we have created a view named EditEmployee as named in the Controller:
Filename:EditEmployee.cshtml
@model Employee;
@{
ViewData["Title"] = "Validation";
var emplyoee = Model;
}
<form>
<div class="form__field field">
<label>Country</label>
<p>@emplyoee.CountryName</p>
<span class="field__validation-message" asp-validation-for="CountryName"></span>
</div>
</form>
Finally the output is obtained as:
The selected country name is displayed in another form as
For more info about Enum visit this link below: Click here