Skip to content

Blazor: 2-way binding to nullable struct not supported. #11519

Open
@alexdrl

Description

@alexdrl

I have a problem with defining Blazor components.

This is my component code...

@code {
    [Parameter]
    public TimeSpan? Value { get; set; }

    [Parameter]
    public EventCallback<TimeSpan?> ValueChanged { get; set; }
...
}

The idea is that I can 2-way bind like this using a TimeSpan? field:

I can't bind it like this:

<TimeSpanTotalHoursSpinEditNullable @bind-Value="@Model.Value" />

With compiler errors:

1>------ Build started: Project: MyApp.WebApp.Client, Configuration: Debug Any CPU ------
1>C:\Users\Username\code\MyCompany\MyApp\src\MyApp.WebApp.Client\obj\Debug\net9.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Components_Adjustments_AdjustmentEditorFormLayout_razor.g.cs(578,421,578,573): error CS1503: Argument 2: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback<System.TimeSpan>' to 'Microsoft.AspNetCore.Components.EventCallback'
1>C:\Users\Username\code\MyCompany\MyApp\src\MyApp.WebApp.Client\obj\Debug\net9.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Components_Adjustments_AdjustmentEditorFormLayout_razor.g.cs(559,154,559,169): error CS1662: Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
1>Done building project "MyApp.WebApp.Client.csproj" -- FAILED.

I can workaround it like this, but seems ugly:

<TimeSpanTotalHoursSpinEditNullable Value="@Model.Value" ValueChanged=@(value => {if (value != null) { Model.Value = value.Value; }} )/>

Is this a compiler issue? Is there a official better way to support having a nullable struct as parameter and binding to it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-compilerUmbrella for all compiler issuesfeature requestLarge improvement request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions