Dropdown does not keep the selection

Dmitriy Reznik 236 Reputation points
2022-06-14T13:41:50.277+00:00

I have a blazor server-side application with a drop-down on a page:

        <EditForm Model="@_dummyEditModel" class="my-4 dropdown-parts">  
            <InputSelect id="products" @bind-Value="_selectedProductId" class="form-control">  
                <option value="@((short)Product.None)">--Select Product--</option>  
                @foreach (QuoteTypeModel product in _products)  
                        {  
                    <option value="@product.Id">@product.Name</option>  
                        }  
            </InputSelect>  
        </EditForm>  

In the code-behind:

            private Product _selectedProductId = Product.None;  
  
            ...  
            public enum Product { None = 0, ShowerPan = 1, LinearDrain = 2, Bench = 3, Niche = 4, Curb = 5 }  

When I run the application, and select an element in the drop-down, the selection appears for a moment, and immediately disappears, leaving the selection empty (though _selectedProductId is assigned correctly). When I select the same element again, the selection stays.

If I make the type of _selectedProductId int instead of enum Product, the drop-down works as expected.
How can I fix this behavior?

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,377 questions
{count} votes