Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Data binding & onchange event not working in Blazor

$
0
0

I created a new Blazor web app project and added a new page to test a select element with both binding and onchange event handling.

Both strategies result in no updates to the attribute.

Here's a minimal reproducible example

@page "/test-select"@inject ILogger<Test> Logger<h3>Select a Project</h3><select @bind="SelectedProject"><option value="">@PlaceholderProjectMessage</option><option value="P1">Project 1</option><option value="P2">Project 2</option><option value="P3">Project 3</option></select><p>Selected Project: @SelectedProject</p><select @onchange="SelectedProjectChanged"><option value="">@PlaceholderProjectMessage</option><option value="P1">Project 1</option><option value="P2">Project 2</option><option value="P3">Project 3</option></select><p>Selected Project: @SelectedProject</p>@code {    private static string PlaceholderProjectMessage { get; } = "Select a Project";    public string SelectedProject { get; set; }    private void SelectedProjectChanged(ChangeEventArgs args)    {        Logger.LogInformation("on change event arrived");        SelectedProject = args.Value?.ToString() ?? PlaceholderProjectMessage;    }}

I'm not sure what I am doing wrong, but both outputs "Selected Project: ..." show the placeholder message without updating whenever I select an option.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>