Skip to content

[Blazor] Preserve null values for <option value="@null"> in single select binding - #67616

Closed
Yuvan111 wants to merge 11 commits into
dotnet:mainfrom
Yuvan111:feature-23934-null-select
Closed

Yuvan111 wants to merge 11 commits into
dotnet:mainfrom
Yuvan111:feature-23934-null-select

Conversation

@Yuvan111

@Yuvan111 Yuvan111 commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

[Blazor] Preserve null values for in single select binding

Description

This change preserves the distinction between null and an empty string when binding a nullable value to a single <select>.

When an <option value="@null"> belongs to a single-select element, Blazor renders:

  • An empty value attribute because the DOM cannot represent a null option value.
  • A data-blazor-null-option marker identifying the option as representing null.

The change-event handler recognizes this marker and reports null to .NET instead of "". A property such as string? therefore receives null directly without requiring manual conversion from an empty string.

Html Output:

Screenshot 2026-07-07 at 10 18 27 AM

Preserving existing fallback behavior

The null marker is emitted only for options belonging to a single <select>.

Options outside that scope retain their existing browser text-content fallback:

  • Options inside <select multiple>.
  • Options inside <datalist>.
  • Options inside an <optgroup> under <select multiple>.
  • Options produced by a RenderFragment under <select multiple> or <datalist>.
  • Standalone options without a single-select ancestor.

Scope and limitations

  • Single-select only. Multi-select binding is not included because selected values are transported as a string[], which cannot preserve null entries end-to-end.
  • Do not mix and in the same single-select. Both render with value="", but only the marker-bearing option reports null. The explicit empty-string option reports "".
  • Nullable target recommended. This behavior is intended for nullable bound values such as string?. A non-nullable target cannot accept the reported null value.
  • Existing behavior remains unchanged outside single-selects. Multi-select and datalist options continue to use browser text-content fallback when their value is null.
  • Selects without a null option are unchanged. They continue reporting the element value, including "" when no option is selected.

Tests

Added unit and browser tests covering null options in single-selects, multi-selects, datalists, optgroups, and render fragments, including duplicate attributes and case-insensitive HTML names.

Fixes #23934

@Yuvan111
Yuvan111 requested a review from a team as a code owner July 6, 2026 07:25
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jul 6, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @Yuvan111. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@Yuvan111 Yuvan111 changed the title [Blazor] Preserve null values for <option value="@null"> in select binding [Blazor] Preserve null values for <option value="@null"> in single select binding Jul 7, 2026
@Youssef1313 Youssef1313 added the area-blazor Includes: Blazor, Razor Components label Sep 2, 2026
Copilot AI lite review requested due to automatic review settings September 23, 2026 03:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The null marker can become incorrect after attribute overrides, and null-bound selections may render incorrectly when the null option is not first.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
What changed in this PR

Adds null-preserving binding for @null options in single-select elements using a DOM marker and JavaScript event handling.

Changes:

  • Emits marker attributes for null option values.
  • Converts marked selections back to null.
  • Adds component, render-tree, and E2E coverage.
File Summary
src/​Components/​Web.JS/​src/​Rendering/​Events/​EventTypes.ts Handles marked single-select values.
src/​Components/​test/​testassets/​BasicTestApp/​SelectNullableOptionsComponent.razor Adds nullable select scenarios.
src/​Components/​test/​testassets/​BasicTestApp/​Index.razor Registers the test component.
src/​Components/​test/​E2ETest/​Tests/​FormsTest.cs Adds browser binding coverage; one nit remains regarding the non-null-to-null transition test.
src/​Components/​Components/​test/​Rendering/​RenderTreeBuilderNullableTest.cs Tests generated nullable-option frames.
src/​Components/​Components/​src/​Rendering/​RenderTreeBuilder.cs Has a critical marker override issue, a moderate initial-selection issue, and a documentation nit.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@PureWeen PureWeen left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Written with GitHub Copilot (AI), which did the code reading and the local test runs described below ("I" refers to those runs). Posted by PureWeen.

Re-checked at 159d050. The stale-marker cleanup from the earlier rounds looks good: the tests fail as expected when I mutate the cleanup code.

Two things from the earlier review are still there at this head, and I reproduced both with small tests:

  • Multi-select: only a direct select[multiple] > option is handled. Options inside an optgroup, a RenderFragment, or a component (including InputSelect<T[]>) still emit the marker.
  • datalist: options wrapped in a render region still emit the marker.

There's also a new point on the behavior change itself, inline.

I didn't run the E2E tests, and I only checked the EventTypes.ts change with a small node script that mirrors the function, not the shipped bundle.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 15, 2026
@Yuvan111 Yuvan111 closed this Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] null in select element

4 participants