Skip to content

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

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

[Blazor] Preserve null values for <option value="@null"> in single select binding#67616
Yuvan111 wants to merge 5 commits into
dotnet:mainfrom
Yuvan111:feature-23934-null-select

Conversation

@Yuvan111

@Yuvan111 Yuvan111 commented Jul 6, 2026

Copy link
Copy Markdown

[Blazor] Preserve null values for in single select binding

Description

<option value="@null"> now binds to a null value on a single <select>, instead of being treated as an empty string. The DOM cannot represent a null <option> value, so we render the value as an empty string and add a small marker attribute that the change handler recognizes. The result is reported to .NET as null, so a nullable bound property receives null directly no manual conversion from "" is needed.

Html Output:

Screenshot 2026-07-07 at 10 18 27 AM

Scope and limitations

  • Single <select> only. Multi-select is not supported in this change, because the array of selected values is delivered as a plain string[] and cannot carry null entries end-to-end.
  • Don't mix <option value=""> and <option value="@null"> in the same <select>. They render identically in the DOM (both as value=""); only the marker-bearing option is reported as null on the change event. The empty-string option will arrive as "". This is by design the two options are indistinguishable to the user and to the DOM, but only the marker option carries the null meaning back to .NET.
  • Nullable target only. The feature is meaningful when the bound property is string?. Binding to a non-nullable string will receive null and surface a runtime error, as expected.
  • Existing behavior preserved. <select> without <option value="@null"> continues to report the element's value exactly as before, including the "" returned when nothing is selected.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] null in select element

1 participant