Skip to content

Fix Inconsistent behavior occurs when using @bind and @bind-value in input date type - #67310

Merged
kotlarmilos merged 19 commits into
dotnet:mainfrom
NanthiniMahalingam:fix-40660
Aug 18, 2026
Merged

kotlarmilos merged 19 commits into
dotnet:mainfrom
NanthiniMahalingam:fix-40660

Conversation

@NanthiniMahalingam

Copy link
Copy Markdown
Contributor

Bug Description

Inconsistent behavior occurs when using @Bind and @bind-value in input date type

Description of code changes

  • Set to default for string empty values if T is nullable or string. For format-based non-nullable types like DateTime, don't update (keep previous value) to avoid resetting to invalid defaults like DateTime.MinValue.
  • Return an empty string (rather than null) so a null nullable date formats the same way an empty date input reports its value to the server. This keeps the rendered 'value' attribute in sync with what the browser already shows, preventing the diff from re-writing it and resetting the caret/segments while the user is typing (e.g. ).

Output

Before changes

Beforefix40660.mp4

After changes

Afterfix40660_1.mp4

Fixes #40660

@NanthiniMahalingam
NanthiniMahalingam marked this pull request as ready for review June 23, 2026 11:12
@NanthiniMahalingam
NanthiniMahalingam requested a review from a team as a code owner June 23, 2026 11:12
Copilot AI review requested due to automatic review settings June 23, 2026 11:12

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.

Pull request overview

Fixes inconsistent <input type="date"> editing behavior in Blazor when using @bind/@bind-value with date/time types by avoiding disruptive re-renders during segmented date entry and aligning formatted null output with what browsers report for empty date inputs.

Changes:

  • Adjusts binder empty-string handling to avoid resetting date/time values to invalid defaults while users are editing.
  • Updates BindConverter formatting for nullable date/time types to return "" (instead of null) so rendered value stays in sync with the browser’s empty value.
  • Adds regression tests covering non-nullable DateTime empty-string scenarios (and should be extended to cover async setter overloads).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/Components/Components/src/EventCallbackFactoryBinderExtensions.cs Updates binder behavior when the browser reports an empty string during change events.
src/Components/Components/src/BindConverter.cs Formats nullable date/time values as empty string to keep rendered value aligned with browser state.
src/Components/Components/test/EventCallbackFactoryBinderExtensionsTest.cs Adds regression tests for non-nullable DateTime empty string binder scenarios.

@sheiksyedm sheiksyedm added area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member labels Aug 12, 2026
@sheiksyedm
sheiksyedm requested a lite review from Copilot August 12, 2026 08:34

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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Suppressed comments (1)

src/Components/Components/src/EventCallbackFactoryBinderExtensions.cs:1517

  • The comment on ShouldSetDefaultOnEmptyString<T>() states that an empty string maps to default(T) for string => null, but the string binder uses ConvertToString which always converts successfully and will call the setter with "" (not null). This makes the comment misleading for readers and future changes; it should describe the default-on-empty behavior for types where conversion fails (e.g., numeric / nullable types), without implying string becomes null.
    // Determines whether an empty string input should reset the bound value to default(T).
    // For most types an empty string maps to default(T) (e.g., string => null, int => 0, int? => null).
    // However, non-nullable date/time types have no meaningful "empty" representation, so resetting them
    // to default (e.g., DateTime.MinValue) would be surprising. For those we leave the existing value
    // unchanged instead.

@kotlarmilos kotlarmilos 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.

LGTM, thanks!

@kotlarmilos
kotlarmilos merged commit fff63de into dotnet:main Aug 18, 2026
29 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Aug 19, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@bind and @bind-Value and @bind-Value for extra model behave different for datetime

4 participants