StaticSelectTagHelper
StaticSelectTagHelper is the tag helper that powers static-for and
static-items on <select> elements. It renders <option> and <optgroup>
tags from a SelectListItem collection, honors authored child content, and
resolves selection from the bound model value first.
This tag helper is used declaratively — apply the static-for attribute
on a <select> inside a Razor template for a component inheriting from
StaticSelect.
Namespace
Section titled “Namespace”TechGems.StaticComponents.Headless.TagHelpersHTML Target Element
Section titled “HTML Target Element”<select static-for="@Model.InputExpression" static-items="@Model.Items"></select>@model PinesSelect
<select static-for="@Model.InputExpression" static-items="@Model.Items" disabled="@(Model.Disabled ? "disabled" : null)" class="…"></select>Public Properties
Section titled “Public Properties”InputExpression
Section titled “InputExpression”[HtmlAttributeName("static-for")]public ModelExpression InputExpression { get; set; }The outer ModelExpression — its .ModelExplorer.Model is expected to be
another ModelExpression bound to the consumer’s actual property.
[HtmlAttributeName("static-items")]public List<SelectListItem> Items { get; set; } = new List<SelectListItem>();The list of items rendered inside the <select>. Items that share a
SelectListGroup reference are rendered together inside a single <optgroup>
at the position of the group’s first occurrence.
Supported Model Types
Section titled “Supported Model Types”Guid, string, int, long, short, byte, float, double, decimal,
DateTime and their nullable counterparts. bool is not supported.
Behavior
Section titled “Behavior”When processed (ProcessAsync), the tag helper:
- Validates the inner model type is supported (throws otherwise).
- Sets
name,idand thedata-val-*attributes throughStaticHeadlessUtils. - Determines whether the bound model value matches at least one item Value (formatted via
FormatModelValuefor invariant-culture comparison). - Walks
Itemsin original order:- Ungrouped items render inline as
<option>tags. - The first item of a group emits an opening
<optgroup label="…">, followed by every item sharing that group reference, followed by</optgroup>. Subsequent occurrences of the same group reference are skipped. - Groups are compared by reference — two
SelectListGroupinstances with the sameNamebut distinct references render as separate<optgroup>blocks. SelectListGroup.Disabledis rendered asdisabledon the<optgroup>.
- Ungrouped items render inline as
- Appends any authored child content (e.g. a leading placeholder
<option>) and writes the combined HTML to the<select>’s content.
Selection priority
Section titled “Selection priority”| Condition | Result |
|---|---|
| Model value matches one of the item Values | That item is selected; every SelectListItem.Selected flag is ignored. |
Model value is null | Fall back to SelectListItem.Selected. |
| Model value does not match any item | Fall back to SelectListItem.Selected. |
Error Conditions
Section titled “Error Conditions”| Condition | Exception |
|---|---|
| Inner property is not one of the supported model types | ArgumentException |
Related
Section titled “Related”- Headless Components — Feature guide.
- StaticSelect