Introduction

In this blog, we will learn how to use the select tag helper in an ASP.NET Core application. We will discuss the select tag helper for a gender dropdown list, Department with SelectListItem, and finally from a database.

Here is the code.

Select Tag Helper: The select tag helper renders as a select tag element with the select element option inside. It can be used alternative to Html.DropdownListFor and Html.ListBoxFor of MVC.

Step 1: Create an ASP.NET web application project in Visual Studio 2019.

Step 2: Create the class employee under Models folder.

  1. using Microsoft.AspNetCore.Mvc.Rendering;
  2. using System.Collections.Generic;
  3. namespace TagHelpers_Demo.Models
  4. {
  5. public class Employee
  6. {
  7. public int Id { get; set; }
  8. public string Gender { get; set; }
  9. public string Department { get; set; }
  10. public List<SelectListItem> Departments { get; } = new List<SelectListItem>
  11. {
  12. new SelectListItem { Value = "1", Text = "Account" },
  13. new SelectListItem { Value = "2", Text = "Finance" },
  14. new SelectListItem { Value = "3", Text = "HR" },
  15. new SelectListItem { Value = "4", Text = "IT" },
  16. new SelectListItem { Value = "5", Text = "Marketing" },
  17. new SelectListItem { Value = "6", Text = "Sales" },
  18. };
  19. }
  20. }

Step 3: “Add” controller or use created HomeController.

  1. public IActionResult Index()
  2. {
  3. var model = new Employee();
  4. model.Departments.ToList();
  5. return View(model);
  6. }
Step 4: Right click on the index method or use the existing index view which is added under the home folder in view folder.
  1. <div class="form-group col-md-4">
  2. <label asp-for="Gender" class="control-lable"></label>
  3. <select asp-for="Gender" class="custom-select">
  4. <option value="">Choose Gender</option>
  5. <option value="1">Male</option>
  6. <option value="2">Female</option>
  7. </select>
  8. </div>
  1. <div class="form-group col-md-4">
  2. <label asp-for="Department" class="control-lable"></label>
  3. <select asp-for="Department" asp-items="Model.Departments" class="custom-select">
  4. <option value="">Choose Department</option>
  5. </select>
  6. </div>

Step 5: “Add” class Department in models folder

  1. using System.ComponentModel.DataAnnotations;
  2. namespace TagHelpers_Demo.Models
  3. {
  4. public class Department
  5. {
  6. [Key]
  7. public int Id { get; set; }
  8. [Required]
  9. [StringLength(100)]
  10. public string DepartmentName { get; set; }
  11. }
  12. }

Step 6: “Add” DbSet in ApplicationDbContext which is located under the data folder of your project.

  1. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  2. using Microsoft.EntityFrameworkCore;
  3. using TagHelpers_Demo.Models;
  4. namespace TagHelpers_Demo.Data
  5. {
  6. public class ApplicationDbContext : IdentityDbContext
  7. {
  8. public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
  9. : base(options)
  10. {
  11. }
  12. public DbSet<Department> Departments { get; set; }
  13. }
  14. }

Step 7: Add-migration in package manager console and update-database. It will add a department table with ID and DepartmentName.

Step 8: Open the same HomeController or a new controller and write the following code on controller class.

  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.AspNetCore.Mvc.Rendering;
  3. using TagHelpers_Demo.Data;
  4. namespace TagHelpers_Demo.Controllers
  5. {
  6. public class HomeController : Controller
  7. {
  8. private readonly ApplicationDbContext dbContext;
  9. public HomeController(ApplicationDbContext context)
  10. {
  11. dbContext = context;
  12. }
  13. public IActionResult Index()
  14. {
  15. ViewData["Departments"] = new SelectList(dbContext.Departments, "Id", "DepartmentName");
  16. return View();
  17. }
  18. }
  19. }

Step 9: Similarly, use the same index view or “Add” a new view if you wish.

  1. @model TagHelpers_Demo.Models.Department
  2. @{
  3. ViewData["Title"] = "Home Page";
  4. }
  5. <form asp-action="">
  6. <div class="form-group col-md-4">
  7. <label asp-for="DepartmentName" class="control-lable"></label>
  8. <select asp-for="Id" asp-items="ViewBag.Id" class="custom-select">
  9. <option value="">Choose Department</option>
  10. </select>
  11. </div>
  12. </form>

Summary

In this blog, I cover select tag helpers with some examples. I hope it will be helpful for your projects.