Blazor InputSelect binds to option text instead of Value

Viewed 931

I use Blazor serverside and I have this:

<InputSelect class="form-control" id="sYear" @bind-Value="@SelectedYear">
    <option value="" selected="@(SelectedYear == "")">Year</option>
    @foreach (int i in Enumerable.Range(admin.GetYearOfGregorianDate(DateTime.Now) - 20, 21).Reverse())
    {
        <option value="@i" selected="@(SelectedYear == i.ToString())">@i.ToString()</option>
    }
</InputSelect>

As is seen, the value for the first option is empty and I expect when I choose the first option the bound value become empty string Not the string "Year" but the SelectedYear is set to "Year"! What to do to solve this?

1 Answers

Just delete the selected tag? Why would you want to use the selected-tag? The value of SelectedYear is bind to the option-value by default.

<InputSelect class="form-control" id="sYear" @bind-Value="@SelectedYear">
    <option value="">Year</option>
    @foreach (int i in Enumerable.Range(admin.GetYearOfGregorianDate(DateTime.Now) - 20, 21).Reverse())
    {
        <option value="@i">@i.ToString()</option>
    }
</InputSelect>
Related