ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

Creating a Drop Down menu using Enum in ASP.net core

.Net ASP.NET about 7 years ago || 11/5/2018 || 5.9 K View

  • Hide

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

 

  • 2
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Binod  Bhandari

Binod Bhandari

Life is not just about thrieving but all about living.

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey