Skip to content

Select tag helper: retarget net10.0, drop the enum wrapper class, fix the Bootstrap select classes - #2182

Open
vladimir-pecanac-main wants to merge 3 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/76850-select-tag-helper
Open

Select tag helper: retarget net10.0, drop the enum wrapper class, fix the Bootstrap select classes#2182
vladimir-pecanac-main wants to merge 3 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/76850-select-tag-helper

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Refresh of aspnetcore-features/SelectTagHelperSolution alongside a rewrite of the article it backs (https://code-maze.com/select-tag-helper-aspnetcore/).

Retarget and packages

  • SelectTagHelper.csproj and Tests.csproj: net6.0 -> net10.0, <Nullable>enable</Nullable> added to both, view-model properties annotated.
  • Microsoft.NET.Test.Sdk 17.1.0 -> 18.9.0, MSTest.TestAdapter and MSTest.TestFramework 2.2.8 -> 4.4.0, coverlet.collector 3.1.2 -> 10.0.1. Versions resolved from NuGet today.

Idiom lifts

  • Program.cs rewritten as top-level statements. The explicit Program class and Main wrapper around WebApplication.CreateBuilder made the sample read as if it predated .NET 6. The test project references HomeController directly and does not use WebApplicationFactory<Program>, so nothing depended on the type.
  • The Enumerations wrapper class around the Department enum is deleted. Department now lives directly in SelectTagHelper.Enums, so the views call Html.GetEnumSelectList<Department>() instead of Html.GetEnumSelectList<Enumerations.Department>(). The article always showed the un-nested form; the repo is the side that was out of step.
  • StaticRepository.cs and HomeController.cs lifted to collection expressions and target-typed new(). This shrinks the code rather than growing it.
  • The article deliberately keeps the explicit new SelectListItem { ... } form in its first example: that snippet exists to show the reader what a SelectListItem is, so the two sides diverge there on purpose.

Bootstrap classes

Every <select> carried class="form-select-sm" with no form-select. wwwroot/lib/bootstrap bundles Bootstrap 5.1.0, where .form-select-sm is a size modifier that styles nothing on its own, which is why the screenshots show unstyled native dropdowns. The pair is now form-select form-select-sm. MultiSelect.cshtml already had form-select and is unchanged.

New placeholder example

A Placeholder action and view render a disabled first item with an empty Value as a placeholder row, so the article's claim about SelectListItem.Disabled has running code behind it. Note that Selected = true on such an item only survives when the bound property's value is already empty: with asp-for, the tag helper computes selection from the model expression and drops the flag when the values do not match.

Build and tests

dotnet build -c Release: succeeded, 0 warnings, 0 errors on SDK 10.0.302.
dotnet test -c Release: Passed - Failed: 0, Passed: 12, Skipped: 0, Total: 12. The 11 existing tests are unchanged and green; the twelfth is the new placeholder assertion.

…tstrap select classes

- SelectTagHelper.csproj and Tests.csproj: net6.0 -> net10.0, nullable enabled
  and view-model properties annotated.
- Test packages lifted: Microsoft.NET.Test.Sdk 18.9.0, MSTest.TestAdapter and
  MSTest.TestFramework 4.4.0, coverlet.collector 10.0.1.
- Program.cs rewritten as top-level statements.
- The Enumerations wrapper class around the Department enum is gone; Department
  now lives directly in SelectTagHelper.Enums, so the views call
  Html.GetEnumSelectList<Department>() and match the article.
- StaticRepository and HomeController lifted to collection expressions and
  target-typed new(). The article keeps the explicit new SelectListItem { ... }
  form in its first example on purpose: that snippet exists to show the reader
  what a SelectListItem is.
- Every select carried class="form-select-sm" with no form-select. Bootstrap
  5.1.0 is bundled in wwwroot/lib, where the -sm modifier styles nothing on its
  own, so the pair is now form-select form-select-sm.
- New Placeholder action and view showing a disabled first item with an empty
  Value as a placeholder row, plus a test asserting it is present, disabled and
  selected. 12 tests pass on net10.0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant