dotnet / dotnet/roslyn

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

Open
#85,589 3 comments 0 reactions 0 assignees View on GitHub
Area-Razor Area-Razor-Compiler Feature Request
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

I have a problem with defining Blazor components.

This is my component code...

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

[Parameter]
public EventCallback 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:

``

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' 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:

` {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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.