Introduction

In this blog, we will discuss ListBox creation using a select tag helper. There is a single value and multiple value select option in the ListBox. We will learn how we can bind it to the database with a step-by-step example.

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

Here is the code.

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

Step 2: Create the class 'Language' under the Models folder.

  1. using Microsoft.AspNetCore.Mvc.Rendering;
  2. using System.Collections.Generic;
  3. namespace ListBox_Demo.Models
  4. {
  5. public class Language
  6. {
  7. public List<SelectListItem> ProgrammingSkill { get; } = new List<SelectListItem>
  8. {
  9. new SelectListItem { Value = "1", Text = "HTML" },
  10. new SelectListItem { Value = "2", Text = "CSS" },
  11. new SelectListItem { Value = "3", Text = "Bootstrap" },
  12. new SelectListItem { Value = "4", Text = "JavaScript" },
  13. new SelectListItem { Value = "5", Text = "JQuery" },
  14. new SelectListItem { Value = "6", Text = "Angular" },
  15. new SelectListItem { Value = "7", Text = "C#" },
  16. new SelectListItem { Value = "8", Text = "ASP.Net MVC" },
  17. new SelectListItem { Value = "9", Text = "ASP.Net Core" },
  18. new SelectListItem { Value = "10", Text = "Entity Framework" },
  19. new SelectListItem { Value = "11", Text = "Identity" },
  20. new SelectListItem { Value = "12", Text = "LINQ" },
  21. new SelectListItem { Value = "13", Text = "SQL Server" },
  22. };
  23. }
  24. }

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

Step 4: Right-click on index method or use the existing index view which is added under the home folder in view folder.

Step 5: “Add” class Skill in models folder

  1. using System.ComponentModel.DataAnnotations;
  2. namespace ListBox_Demo.Models
  3. {
  4. public class Skill
  5. {
  6. [Key]
  7. public int Id { get; set; }
  8. [Required]
  9. [StringLength(100)]
  10. [Display(Name = "Skills")]
  11. public string ProgrammingLanguage { get; set; }
  12. }
  13. }

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

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

Step 7: Add-migration in the package manager console and update-database. It will be added department table with ID and ProgrammingLanguage.

Step 8: Open the same HomeController or a new controller and write the following code in the controller class.
  1. using ListBox_Demo.Data;
  2. using ListBox_Demo.Models;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.Rendering;
  5. using System.Linq;
  6. namespace ListBox_Demo.Controllers
  7. {
  8. public class HomeController : Controller
  9. {
  10. private readonly ApplicationDbContext dbContext;
  11. public HomeController(ApplicationDbContext context)
  12. {
  13. dbContext = context;
  14. }
  15. public IActionResult Index()
  16. {
  17. ViewData["Id"] = new SelectList(dbContext.Skills, "Id", "ProgrammingLanguage");
  18. return View();
  19. }
  20. public IActionResult Skills()
  21. {
  22. var model = new Language();
  23. model.ProgrammingSkill.ToList();
  24. return View(model);
  25. }
  26. }
  27. }

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

Index View:

  1. @model ListBox_Demo.Models.Skill
  2. @{
  3. ViewData["Title"] = "Home";
  4. }
  5. <div class="form-group col-md-4">
  6. <select asp-for="Id" asp-items="ViewBag.Id" size="5" multiple class="form-control"></select>
  7. <span asp-validation-for="ProgrammingLanguage" class="text-danger"></span>
  8. </div>

Skills View:
  1. @model ListBox_Demo.Models.Language
  2. @{
  3. ViewData["Title"] = "Skills";
  4. }
  5. <div class="form-group col-md-4">
  6. <select asp-for="ProgrammingSkill" asp-items="Model.ProgrammingSkill" size="5" multiple class="form-control"></select>
  7. <span asp-validation-for="ProgrammingSkill" class="text-danger"></span>
  8. </div>