46
votes

So I have been stuck trying to get a simple onchange to fire when a select dropdown value changes. Like so:

<select class="form-control d-flex" onchange="(dostuff())">
    @foreach (var template in templatestate.templates)
    {
        <option [email protected]>@template.Name</option>
    }
</select>

with the method being called:

void dostuff()
{
   Console.WriteLine("first spot is firing");
    _template = templatestate.templates.FirstOrDefault(x => x.Name == 
    _template.Name);
    Console.WriteLine("second spot is firing");
}

The result I get it no matter how I try to reorient it is this error in the browser.

Uncaught Error: System.ArgumentException: There is no event handler with ID 0

Is there something obvious and key that I am missing? Because I have a button onclick event that works just fine in the same page.

4
which version of blazor you are using, 0.1.0 or 0.2.0VibeeshanRC
I am using 0.2.1Grand Masta1

4 Answers

59
votes

Your answer should be in the cshtml:

<select onchange=@DoStuff>
    @foreach (var template in templates)
    {
        <option value=@template>@template</option>
    }
</select>

Then your @functions (in razor components @code instead. See: https://docs.microsoft.com/vi-vn/aspnet/core/mvc/views/razor?view=aspnetcore-3.0#functions) should look like:

@functions {
    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"> 

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

Note that in Preview 6, it appears that syntax will be @onchange="@DoStuff" with the future promising @onchange="DoStuff". Here's a link to some changes: Blazor Preview 6

25
votes

As an alternative to setting an onchange event, you could just bind the dropdown to a property and handle changes in the property set. This way you get the value being selected all in the same process and without having to convert an object value. Plus if you're already using @bind on the select, you are prevented from using onchange on it as well.

<select @bind="BoundID">
 ...
</select>

@code {
  private int? _boundID = null;
  private int? BoundID
  {
    get
    {
      return _boundID;
    }
    set
    {
      _boundID = value;
     //run your process here to handle dropdown changes
    }
  }
}
4
votes

Above answer didn't work for me, got compilation error.

below is my working code.

@inject HttpClient httpClient

@if (States != null)
{

<select id="SearchStateId" name="stateId" @onchange="DoStuff" class="form-control1">
    <option>@InitialText</option>
    @foreach (var state in States)
    {
        <option value="@state.Name">@state.Name</option>
    }
</select>
}


@code {
[Parameter] public string InitialText { get; set; } = "Select State";
private KeyValue[] States;
private string selectedString { get; set; }
protected override async Task OnInitializedAsync()
{
    States = await httpClient.GetJsonAsync<KeyValue[]>("/sample-data/State.json");
}

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

public class KeyValue
{
    public int Id { get; set; }

    public string Name { get; set; }
}
}
3
votes

For starters you are not using the correct bind syntax:

onchange="@dostuff"

notice the @