asp.net-coreblazor

Why blazor select/inputselect is not working in a new app?


I just created a new .net blazor web app project and added this code

@page "/weather"

@attribute [StreamRendering] @rendermode InteractiveServer <PageTitle>Weather</PageTitle>

<h1>Weather</h1>

@using System.Diagnostics

<h3>Dropdown Example</h3>

<select @bind="SelectedSet" class="form-select">
    <option disabled selected value="">-- Select a Set --</option>
    @foreach (var set in Sets)
    {
        <option value="@set">@set</option>
    } </select> <p>You selected: @SelectedSet</p>

<div class="mb-3">
    <label for="SetType" class="form-label">Set Type</label>
    <InputSelect id="SetType" @bind-Value="CollectionSet" class="form-select">
        @foreach (var set in Sets)
        {
            <option value="@set">@set</option>
        }
    </InputSelect> </div> <p>You selected: @CollectionSet</p>

@code {
    private List<string> Sets = new List<string> { "Pad", "Leather", "Bronze" };
    private string SelectedSet { get; set; }
    private string CollectionSet { get; set; }

    private void OnSetChanged(ChangeEventArgs e)
    {
        Debug.WriteLine($"Selected value changed to: {SelectedSet}");
        // Additional logic can be added here
    } }

but when i put a breakpoint on the methods it is just not working I can't figured out why the event is not triggering. At first I used an enum and i tought it was because of that, but for a simple string it is still not working


Solution

  • Your (first) code should work already. When it doesn't you probably don't have an interactivity set. Add this line to the top of the file:

    @rendermode InteractiveServer
    

    The UI should update with the selection. But note that OnParametersSet() is not a useful place to trace this, I expect the Debug.WriteLine() to only happen once.