dotnet / dotnet/aspnetcore

Blazor InputSelect sets wrong selected Option

Open
#41,222 13 comments 3 reactions 0 assignees View on GitHub
area-blazor bug feature-blazor-builtin-components Pillar: Technical Debt Priority:2
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

Let's say that we have a really simple class with 2 properties, one is an integer and the other one is a string. I want to use EditForm with 2 InputSelect elements that are bind to these properties. The code looks like this:

```razor
@page "/test"
@using Microsoft.AspNetCore.Components
@using Newtonsoft.Json

Test

Test form



String

A
B
C


Integer

1
2
3

Start

@code {
readonly Dummy _dummy = new();

private void TestAsync()
{
Console.WriteLine("Object state: " + JsonConvert.SerializeObject(_dummy));
}

class Dummy
{
public string? StringProp { get; set; }
public int IntProp { get; set; }
}

}
```

Here is how it looks like in a web browser:

![image](https://user-images.githubusercontent.com/6372824/163680842-698615f1-e3e2-4867-89da-330844f01181.png)

As we can see, if property is nullable, then Blazor will set the first option as selected. When we submit this form, the actual value of that property will not be the value of the selected option, it will be null.

### Expected Behavior

When we bind InputSelect to nullable property, it should not select any option by default. The behavior should be the same as with non-nullable properties

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

6.0

### Anything else?

_No response_

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.