In the Blazor WebAssembly application, the @onchange event attached to the element fails to trigger when selecting the “Weather” option. The goal is to dynamically render components based on the selected option, but the event doesn’t respond as expected.
@page "/"
<PageTitle>Home</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<h2>Dynamic Component</h2>
<select @onchange="ChangeValue"> /*Doesn't fire */
<option value="Default">select</option>
<option value="Weather">Weather</option>
</select>
@if(selectedType is not null)
{
<div class="p-2 mt-2">
<DynamicComponent Type="selectedType"/>
</div>
}
else
{
<h3>Waiting...</h3>
}
@code{
private Type? selectedType;
private Dictionary<String, Type> components = new Dictionary<string, Type>()
{
["Weather"] = typeof(Weather)
};
public void ChangeValue(ChangeEventArgs e)
{
Console.WriteLine("entre");
if (e.Value is String DropdownValue)
{
selectedType = components[DropdownValue];
}
else
{
selectedType = null;
}
}
}
New contributor
Johhan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.