Olson CloudWorks 🚀

Select Tag Helper in ASPNET Core MVC

September 19, 2026

Select Tag Helper in ASPNET Core MVC

In the dynamic world of ASP.NET Core MVC development, efficiently handling user input is paramount. The Select Tag Helper is a powerful tool that simplifies the creation of dropdown lists, providing a clean and concise way to bind data to your views. Instead of writing verbose HTML markup, you can leverage the Select Tag Helper to dynamically generate <select> elements, making your code more readable and maintainable. This tool reduces boilerplate code and streamlines the process of creating dynamic and interactive web forms, allowing developers to focus on the core logic of their applications. Understanding how to properly utilize the Select Tag Helper can significantly improve your productivity and the overall quality of your ASP.NET Core MVC projects. We’ll delve into its features, benefits, and practical applications.

Understanding the Select Tag Helper

The Select Tag Helper in ASP.NET Core MVC is an attribute you can use on <select> elements to automatically generate the dropdown list options based on a model property. This eliminates the need to manually create <option> tags within your views. It’s a server-side helper, meaning the code executes on the server and generates standard HTML, making it compatible with all browsers. The helper simplifies the binding of data from your model to the dropdown list, making it easier to manage and update your forms. By using the Select Tag Helper, you can create more dynamic and user-friendly web applications.

The core benefit of the Select Tag Helper lies in its ability to reduce code duplication and improve the maintainability of your views. Imagine having to manually update the options in multiple dropdown lists across your application; it would be a tedious and error-prone process. With the Select Tag Helper, you can centralize the data source for your dropdown lists, ensuring consistency and simplifying updates. The helper uses reflection to inspect the model property and generate the necessary HTML, allowing for cleaner and more readable code. For instance, if you have a list of countries stored in your model, the Select Tag Helper can automatically generate the <option> tags for each country in the list.

Consider this example: You have a model with a property called “Country” that is a string, and you want to create a dropdown list of available countries. Instead of manually creating the <select> and <option> tags, you can use the Select Tag Helper to bind the dropdown list to the “Country” property. This not only simplifies your view but also ensures that the selected value is automatically bound back to the model when the form is submitted. This seamless integration with the model binding process is one of the key advantages of using the Select Tag Helper. According to Microsoft documentation, using Tag Helpers promotes cleaner code compared to older HTML Helpers [Microsoft Tag Helpers Documentation].

Implementing the Select Tag Helper

Implementing the Select Tag Helper involves a few key steps. First, you need to ensure that you have a model with a property that represents the data you want to display in the dropdown list. This property can be a simple list of strings, an enum, or a more complex object. Second, you need to use the <select> element in your view and add the asp-for attribute, which specifies the model property to bind to. Finally, you may need to provide the data source for the dropdown list using the asp-items attribute. The asp-items attribute takes an IEnumerable<SelectListItem>, which represents the list of options to display in the dropdown.

Here’s a step-by-step guide to implementing the Select Tag Helper:

  1. Define your model with a property for the selected value and a property for the list of options.
  2. Create a controller action that populates the list of options and passes the model to the view.
  3. In your view, use the <select> element with the asp-for and asp-items attributes to bind the dropdown list to the model properties.
  4. Handle the form submission in your controller to process the selected value.

Let’s look at a code example. Suppose you have a view model like this:

public class MyViewModel { public string SelectedOption { get; set; } public List<SelectListItem> Options { get; set; } } 

And in your controller, you populate the Options list and pass the model to the view. Then, in your view, you would use the following code:

<select asp-for="SelectedOption" asp-items="@Model.Options"></select> 

This simple code snippet is all you need to generate a dynamic dropdown list using the Select Tag Helper. This drastically simplifies your Razor views and makes them much easier to read. The result is cleaner code and faster development cycles. According to a Stack Overflow survey, developers using ASP.NET Core report increased productivity due to features like Tag Helpers [Stack Overflow Developer Survey 2022].

Advanced Usage and Customization

The Select Tag Helper offers several advanced features and customization options. You can use data annotations to validate the selected value, customize the display text and values of the options, and even create cascading dropdown lists. Cascading dropdown lists are dropdown lists that depend on the selected value of another dropdown list. For example, you might have a dropdown list of countries, and then a second dropdown list of cities that only shows cities in the selected country. This requires using JavaScript or AJAX to dynamically update the options in the second dropdown list based on the selected value of the first dropdown list. This allows for dynamic and interactive user interfaces.

One powerful feature is the ability to use enums as the data source for the dropdown list. When you use an enum, the Select Tag Helper automatically generates the <option> tags for each enum value, using the enum’s display name as the text and the enum’s value as the value. This can significantly simplify the process of creating dropdown lists for enums. However, you can also use custom attributes or display names to further refine how the enum values are displayed in the dropdown list. For example, you can use the [Display(Name = "Custom Text")] attribute on the enum values to specify a custom display name. This provides greater flexibility and control over the appearance of your dropdown lists.

To customize the appearance of the dropdown list, you can use standard CSS classes. The Select Tag Helper generates standard HTML, so you can apply any CSS styles you want to the <select> element and the <option> tags. This allows you to create dropdown lists that match the overall design of your application. You can also use JavaScript to add additional functionality to the dropdown list, such as autocomplete or filtering. For example, you can use a JavaScript library like Select2 to enhance the user experience of your dropdown lists. According to a study by Nielsen Norman Group, well-designed dropdown lists can improve user satisfaction and reduce errors [Nielsen Norman Group on Dropdown Usability].

Here’s a paragraph optimized as a featured snippet:

The ASP.NET Core MVC Select Tag Helper simplifies dropdown list creation by automatically generating <option> tags based on your model data. Instead of manually coding each option, you bind your model property to the <select> element using the asp-for attribute and provide the data source using the asp-items attribute. This reduces boilerplate code, improves maintainability, and ensures consistency across your application, making it an essential tool for efficient web development.

Benefits and Best Practices

Using the Select Tag Helper offers numerous benefits, including reduced code complexity, improved maintainability, and increased productivity. By automating the generation of dropdown lists, you can focus on the core logic of your application and avoid writing repetitive HTML code. This also makes it easier to update and maintain your views, as you only need to modify the data source for the dropdown list, rather than manually updating each <option> tag. Furthermore, the Select Tag Helper promotes consistency across your application, ensuring that all dropdown lists have the same look and feel.

Here are some best practices to follow when using the Select Tag Helper:

  • Always use a view model to pass data to your views. This helps to keep your views clean and maintainable.

  • Use enums whenever possible for dropdown lists with a fixed set of options. This ensures type safety and makes it easier to manage the options.

  • Use data annotations to validate the selected value. This helps to prevent invalid data from being submitted to your application.

  • Ensure your model properties are correctly decorated to provide the necessary information for the Tag Helper.

  • Consider using caching to improve the performance of your dropdown lists, especially if the data source is large or frequently accessed.

Properly utilizing the Select Tag Helper can significantly streamline your development workflow and improve the overall quality of your ASP.NET Core MVC applications. By adhering to best practices and leveraging the advanced features of the helper, you can create dynamic, user-friendly, and maintainable web forms. Remember, the goal is to write less code and achieve more, and the Select Tag Helper is a valuable tool in achieving that goal. It also helps to make your code more testable and resilient to change. You can also link to other parts of your application using relevant internal pages.

Infographic here
FAQ About the Select Tag Helper -------------------------------
What is the Select Tag Helper?
The Select Tag Helper is an ASP.NET Core MVC feature that simplifies the creation of dropdown lists by automatically generating `
How do I use the Select Tag Helper?
Use the `` element and `
By mastering the **Select Tag Helper**, you're not just simplifying dropdown creation, you're embracing a more efficient and maintainable approach to ASP.NET Core MVC development. Take the knowledge you've gained here and apply it to your projects. Experiment with different data sources, explore advanced customization options, and discover how the **Select Tag Helper** can streamline your workflow. Don't hesitate to dive deeper into the official documentation and community resources to uncover even more tips and tricks. The more you practice, the more proficient you'll become, leading to cleaner code, faster development cycles, and ultimately, more successful web applications.

Question & Answer :
I need some help with the select tag helper in ASP.NET Core.

I have a list of employees that I’m trying to bind to a select tag helper. My employees are in a List<Employee> EmployeesList and selected value will go into EmployeeId property. My view model looks like this:

public class MyViewModel { public int EmployeeId { get; set; } public string Comments { get; set; } public List<Employee> EmployeesList {get; set; } } 

My employee class looks like this:

public class Employee { public int Id { get; set; } public string FullName { get; set; } } 

My question is how do I tell my select tag helper to use the Id as the value while displaying FullName in the drop down list?

<select asp-for="EmployeeId" asp-items="???" /> 

I’d appreciate some help with this. Thanks.

Using the Select Tag helpers to render a SELECT element

In your GET action, create an object of your view model, load the EmployeeList collection property and send that to the view.

public IActionResult Create() { var vm = new MyViewModel(); vm.EmployeesList = new List<Employee> { new Employee { Id = 1, FullName = "Shyju" }, new Employee { Id = 2, FullName = "Bryan" } }; return View(vm); } 

And in your create view, create a new SelectList object from the EmployeeList property and pass that as value for the asp-items property.

@model MyViewModel <form asp-controller="Home" asp-action="Create"> <select asp-for="EmployeeId" asp-items="@(new SelectList(Model.EmployeesList, nameof(Employee.Id), nameof(Employee.FullName)))"> <option>Please select one</option> </select> <input type="submit"/> </form> 

And your HttpPost action method to accept the submitted form data.

[HttpPost] public IActionResult Create(MyViewModel model) { // check model.EmployeeId // to do : Save and redirect } 

Or

If your view model has a List<SelectListItem> as the property for your dropdown items.

public class MyViewModel { public int EmployeeId { get; set; } public string Comments { get; set; } public List<SelectListItem> Employees { set; get; } } 

And in your get action,

public IActionResult Create() { var vm = new MyViewModel(); vm.Employees = new List<SelectListItem> { new SelectListItem {Text = "Shyju", Value = "1"}, new SelectListItem {Text = "Sean", Value = "2"} }; return View(vm); } 

And in the view, you can directly use the Employees property for the asp-items.

@model MyViewModel <form asp-controller="Home" asp-action="Create"> <label>Comments</label> <input type="text" asp-for="Comments"/> <label>Lucky Employee</label> <select asp-for="EmployeeId" asp-items="@Model.Employees" > <option>Please select one</option> </select> <input type="submit"/> </form> 

The class SelectListItem belongs to Microsoft.AspNet.Mvc.Rendering namespace.

Make sure you are using an explicit closing tag for the select element. If you use the self closing tag approach, the tag helper will render an empty SELECT element!

The below approach will not work

<select asp-for="EmployeeId" asp-items="@Model.Employees" /> 

But this will work.

<select asp-for="EmployeeId" asp-items="@Model.Employees"></select> 

Getting data from your database table using entity framework

The above examples are using hard coded items for the options. So i thought i will add some sample code to get data using Entity framework as a lot of people use that.

Let’s assume your DbContext object has a property called Employees, which is of type DbSet<Employee> where the Employee entity class has an Id and Name property like this

public class Employee { public int Id { set; get; } public string Name { set; get; } } 

You can use a LINQ query to get the employees and use the Select method in your LINQ expression to create a list of SelectListItem objects for each employee.

public IActionResult Create() { var vm = new MyViewModel(); vm.Employees = context.Employees .Select(a => new SelectListItem() { Value = a.Id.ToString(), Text = a.Name }) .ToList(); return View(vm); } 

Assuming context is your db context object. The view code is same as above.

Using SelectList

Some people prefer to use SelectList class to hold the items needed to render the options.

public class MyViewModel { public int EmployeeId { get; set; } public SelectList Employees { set; get; } } 

Now in your GET action, you can use the SelectList constructor to populate the Employees property of the view model. Make sure you are specifying the dataValueField and dataTextField parameters. You can use a nameof expression to link the field names statically.

public IActionResult Create() { var vm = new MyViewModel(); vm.Employees = new SelectList(GetEmployees(), nameof(Employee.Id), nameof(Employee.FirstName)); return View(vm); } public IEnumerable<Employee> GetEmployees() { // hard coded list for demo. // You may replace with real data from database to create Employee objects return new List<Employee> { new Employee { Id = 1, FirstName = "Shyju" }, new Employee { Id = 2, FirstName = "Bryan" } }; } 

Here I am calling the GetEmployees method to get a list of Employee objects, each with an Id and FirstName property and I use those properties as DataValueField and DataTextField of the SelectList object we created. You can change the hardcoded list to a code which reads data from a database table.

The view code will be same.

<select asp-for="EmployeeId" asp-items="@Model.Employees" > <option>Please select one</option> </select> 

Render a SELECT element from a list of strings.

Sometimes you might want to render a select element from a list of strings. In that case, you can use the SelectList constructor which only takes IEnumerable<T>

var vm = new MyViewModel(); var items = new List<string> {"Monday", "Tuesday", "Wednesday"}; vm.Employees = new SelectList(items); return View(vm); 

The view code will be same.

Setting selected options

Some times,you might want to set one option as the default option in the SELECT element (For example, in an edit screen, you want to load the previously saved option value). To do that, you may simply set the EmployeeId property value to the value of the option you want to be selected.

public IActionResult Create() { var vm = new MyViewModel(); vm.Employees = new List<SelectListItem> { new SelectListItem {Text = "Shyju", Value = "11"}, new SelectListItem {Text = "Tom", Value = "12"}, new SelectListItem {Text = "Jerry", Value = "13"} }; vm.EmployeeId = 12; // Here you set the value return View(vm); } 

This will select the option Tom in the select element when the page is rendered.

Multi select dropdown

If you want to render a multi select dropdown, you can simply change your view model property which you use for asp-for attribute in your view to an array type.

public class MyViewModel { public int[] EmployeeIds { get; set; } public List<SelectListItem> Employees { set; get; } } 

This will render the HTML markup for the select element with the multiple attribute which will allow the user to select multiple options.

@model MyViewModel <select id="EmployeeIds" multiple="multiple" name="EmployeeIds"> <option>Please select one</option> <option value="1">Shyju</option> <option value="2">Sean</option> </select> 

Setting selected options in multi select

Similar to single select, set the EmployeeIds property value to the an array of values you want.

public IActionResult Create() { var vm = new MyViewModel(); vm.Employees = new List<SelectListItem> { new SelectListItem {Text = "Shyju", Value = "11"}, new SelectListItem {Text = "Tom", Value = "12"}, new SelectListItem {Text = "Jerry", Value = "13"} }; vm.EmployeeIds= new int[] { 12,13} ; return View(vm); } 

This will select the option Tom and Jerry in the multi select element when the page is rendered.

Using ViewBag to transfer the list of items

If you do not prefer to keep a collection type property to pass the list of options to the view, you can use the dynamic ViewBag to do so.(This is not my personally recommended approach as viewbag is dynamic and your code is prone to uncaught typo errors)

public IActionResult Create() { ViewBag.Employees = new List<SelectListItem> { new SelectListItem {Text = "Shyju", Value = "1"}, new SelectListItem {Text = "Sean", Value = "2"} }; return View(new MyViewModel()); } 

and in the view

<select asp-for="EmployeeId" asp-items="@ViewBag.Employees"> <option>Please select one</option> </select> 

Using ViewBag to transfer the list of items and setting selected option

It is same as above. All you have to do is, set the property (for which you are binding the dropdown for) value to the value of the option you want to be selected.

public IActionResult Create() { ViewBag.Employees = new List<SelectListItem> { new SelectListItem {Text = "Shyju", Value = "1"}, new SelectListItem {Text = "Bryan", Value = "2"}, new SelectListItem {Text = "Sean", Value = "3"} }; vm.EmployeeId = 2; // This will set Bryan as selected return View(new MyViewModel()); } 

and in the view

<select asp-for="EmployeeId" asp-items="@ViewBag.Employees"> <option>Please select one</option> </select> 

Grouping items

The select tag helper method supports grouping options in a dropdown. All you have to do is, specify the Group property value of each SelectListItem in your action method.

public IActionResult Create() { var vm = new MyViewModel(); var group1 = new SelectListGroup { Name = "Dev Team" }; var group2 = new SelectListGroup { Name = "QA Team" }; var employeeList = new List<SelectListItem>() { new SelectListItem() { Value = "1", Text = "Shyju", Group = group1 }, new SelectListItem() { Value = "2", Text = "Bryan", Group = group1 }, new SelectListItem() { Value = "3", Text = "Kevin", Group = group2 }, new SelectListItem() { Value = "4", Text = "Alex", Group = group2 } }; vm.Employees = employeeList; return View(vm); } 

There is no change in the view code. the select tag helper will now render the options inside 2 optgroup items.