Skip to content

InputSelect does not bind correctly to Nullable<bool> #54565

Description

@petergolde

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

If you create a InputSelect control that is bound (via @bind-Value) to a bool?, it does not work in the same way as InputSelect bound to other nullable types (say int or Enum), and it never displays the null value correctly, but always shows the "false" value.

Expected Behavior

When a Nullable is bound to an InputSelect, the option with value="" should be bound to the null value. This is how other nullable types work with InputSelect, and also is the most useful.

If the model has null, then the option with value="" should be displayed when the form initially shows.

If the select is manually set to the option with value="", then that value should continue to be displayed.

Steps To Reproduce

@page "/"

<EditForm Model="Model" OnValidSubmit="Submit">
    <p>
        <label>What is your favorite color?</label>
        <InputSelect @bind-Value="Model.FavoriteColor">
            <option value="">Select a Color</option>
            <option value="Red">Red</option>
            <option value="Green">Green</option>
            <option value="Blue">Blue</option>
        </InputSelect>
    </p>

    <p>
        <label>How many legs do you have?</label>
        <InputSelect @bind-Value="Model.NumberOfLegs">
            <option value="">Select a Number</option>
            <option value="1">1</option>
            <option value="2">2</option>
        </InputSelect>
    </p>

    <p>
        <label>Are you awesome?</label>
        <InputSelect @bind-Value="Model.Awesome">
            <option value="">Select Yes or No</option>
            <option value="true">Yes</option>
            <option value="false">No</option>
        </InputSelect>
    </p>

    <button>Submit</button>
</EditForm>

@code {
    enum Color { Red, Green, Blue };
    class InputModel
    {
        public Color? FavoriteColor { get; set; } = null;
        public int? NumberOfLegs { get; set; } = null;
        public bool? Awesome { get; set; } = null;
    }

    InputModel Model = new InputModel();

    void Submit()
    { 
        Console.WriteLine($"Color: {Model.FavoriteColor} Legs: {Model.NumberOfLegs} Awesome: {Model.Awesome}");
    }
}

When this form is initially shown, the Favorite Color and Number of Legs selects show the "Select..." option, but the Awesome select shows "No". In addition, any attempt to set the "Awesome" select to "Select Yes or No" has it change immediate back to No.

Exceptions (if any)

No response

.NET Version

8.0.200

Anything else?

I think the fix is to change the src/Components/Web/src/Forms/InputSelect.cs implementation of FormatValueAsString to something like:

        protected override string? FormatValueAsString(TValue? value)
        {
            if (typeof(TValue) == typeof(bool)) {
                return (bool)(object)value! ? "true" : "false";
            }
            else if (typeof(TValue) == typeof(bool?)) {
                if (value == null)
                    return null;
                else 
                    return (bool)(object)value ? "true" : "false";
            }

            return base.FormatValueAsString(value);
        }

Luckily this method is protected, so I can fix this in the meantime by creating my own component:

    public class BetterInputSelect<TValue>: InputSelect<TValue>
    {
        protected override string? FormatValueAsString(TValue? value)
        {
            if (typeof(TValue) == typeof(bool)) {
                return (bool)(object)value! ? "true" : "false";
            }
            else if (typeof(TValue) == typeof(bool?)) {
                if (value == null)
                    return null;
                else 
                    return (bool)(object)value ? "true" : "false";
            }

            return base.FormatValueAsString(value);
        }
    }

But I think the actual InputSelect.cs should be fixed.

Activity

  1. ghost added
    area-blazor
    on Mar 15, 2024
  2. added
    bug
    feature-blazor-builtin-components
    on Mar 20, 2024
  3. added this to the Backlog milestone on Mar 20, 2024
  4. robinwilson16 commented on Dec 11, 2024

    @robinwilson16

    Please could this be fixed as otherwise it makes forms not really work if you want to require users to pick a value:
    Image

  5. robinwilson16 commented on Dec 11, 2024

    @robinwilson16

    Thanks @petergolde I can confirm your fix works and makes it work as I believe is intended so that it picks the "" option for bool? and not false.
    Perhaps Microsoft can merge this fix into the main code for Christmas!

  6. Inspirateur commented on Apr 23, 2025

    @Inspirateur

    Just lost 2 work hours running into this. This should be a core issue and it's still open after 1 year ?
    Sorry for being salty but this really feels like it should be fixed already.

  7. m-e-lloyd commented on Jan 23, 2026

    @m-e-lloyd

    Same issue still persists. Can confirm that the suggested fix works nicely.

  8. robinwilson16 commented on Jan 25, 2026

    @robinwilson16

    Please can this bug be patched in .NET so bool? selects work correctly without defaulting to the false option.
    It didn't used to do this so I think was broken with .NET 8 but remains broken in .NET 10 (and .NET 9).

    Perhaps it can be fixed in .NET 11 before next Christmas?!

    Having yes/no options on web forms that are NULL and required is a pretty standard requirement and shouldn't need people to work around the bug after troubleshooting their code and realising it is the framework at fault.

  9. m-e-lloyd commented on Jan 25, 2026

    @m-e-lloyd

    Have submitted a PR to fix this, hopefully it gets picked up.

  10. sweeperq commented on Mar 4, 2026

    @sweeperq

    Just ran into this today. Apparently, it is still an issue almost 2 years later.

  11. amelia635 commented on May 6, 2026

    @amelia635

    Also ran into this today.

  12. thomak-dev commented on Jul 6, 2026

    @thomak-dev

    Please fix this already, I am tired of creating tristate enums just because of this

  13. silverlight-enjoyer commented on Aug 26, 2026

    @silverlight-enjoyer

    Chiming into say that I also am running into this and is quite annoying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorbugfeature-blazor-builtin-componentsfeature-model-binding

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions