Render a linked (chained) dropdown¶
Emit a <select> whose visible options are filtered client-side by a parent field's value. Trigger: Razor view calling the helper.
| Field | Value |
|---|---|
| Operation ID | OP-21 |
| Trigger | Razor view calling the helper |
| Inputs | Field name or lambda expression, parent field id, IEnumerable<LinkedSelectListItem>, removeDefault, html attributes |
| Outputs | MvcHtmlString <select> markup |
| Primary components | LinkedDropdownHelper.LinkedDropdownList and LinkedDropdownListFor |
| Entry point | src/Web.Mvc/UI/Extensions/LinkedDropdownHelper.cs |
Steps¶
LinkedDropdownListForconverts the lambda to a property name throughExpressionHelper.GetExpressionTextand delegates toLinkedDropdownList.- Compute the element
idby replacing.with_in the name (BR-47). - Build
<select>withid,name, anddata-parent-id="{parent}", then merge caller html attributes throughObjectExtensions.MergeAttributes, which concatenates rather than overwrites by default (BR-48). - Add CSS classes
{parent}andjs-bo-ddl-chained— the client-side filter hook.* - For each item, skip it when
removeDefaultis true andValue == "-1"(the conventional "please select" entry); otherwise emit an<option>whose CSS class is the item'sLinkValue, withvalue, inner text, andselectedwhenSelectedis set (BR-49, BR-50). LinkedSelectList.GetListItemsbuilds those items: it guardsitemsnon-null throughEnforce, builds a case-insensitiveHashSetof selected values, and for each source object evaluatesDataValueField,DataTextField, andDataLinkedValueFieldthroughDataBinder.Eval(returning the object itself when the field expression is empty), markingSelectedwhen the value is in the selected set (BR-51).
Process flow¶
The following diagram visualizes this operation's control flow.
flowchart TD
Start([Razor view calls LinkedDropdownList])
Id[Compute sanitized element id]
Build[Build select with data-parent-id]
Merge[Merge caller html attributes]
Items[Evaluate GetListItems]
Filter{removeDefault and value -1?}
Skip([Skip option])
Emit[Emit option element]
EndOk([Return select markup])
Start --> Id
Id --> Build
Build --> Merge
Merge --> Items
Items --> Filter
Filter -->|Yes| Skip
Filter -->|No| Emit
Emit --> EndOk
Skip --> EndOk
Failure and edge-case behavior¶
- No unexpected failure paths beyond argument-guard exceptions and delegated dependency behavior were identified for this operation.
Related documentation¶
Note: Statements marked with
*were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts. Statements marked with**could not be confirmed; treat as unknown until verified.
Sources¶
src/Web.Mvc/UI/Extensions/LinkedDropdownHelper.cs