Blazor onchange event with select dropdown

Your answer should be in the cshtml:

<select @onchange="DoStuff">  //pre-3.0 versions: onchange=@DoStuff
    @foreach (var template in templates)
    {
        <option value=@template>@template</option>
    }
</select>

Then your @functions (in razor components @code instead. See: Razor Syntax: Functions) should look like:

@functions {  //use @code in razor components.
    List<string> templates = new List<string>() { "Maui", "Hawaii", "Niihau", "Kauai", "Kahoolawe" };
    string SelectedString = "Maui";

    void DoStuff(ChangeEventArgs e)
    {
        SelectedString = e.Value.ToString();
        Console.WriteLine("It is definitely: " + SelectedString);
    }
}

You could also just use a bind…

<select @bind="SelectedString"> //pre 3.0 bind="@SelectedString"

but @onchange=”DoStuff” allows you to perform logic on selection.

Here’s a link to some changes: Blazor WebAssembly 3.2.0 Preview 5 release now available

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)