drop-down-menuhtml-selectonchangeblazorblazor-server-side

Blazor select dropdown set active value by code


I am dynamically filling the values of a dropdown list based of the content of a list. When a item in the dropdown is selected, an option to remove this item is shown. When the item is removed, it is first removed from the list and then the dropdown list is rebuilt, this is where I run in to problems.

Instead of returning the dropdown to its default value when it is rebuilt the value just below the removed one is shown as selected (this happens without the @onchange value being triggered).

How can I make the dropdown list return to its default value when it is being rebuilt?

Here is some Razor code:

<select class="form-control" @onchange="selectedValue">
   <option value="">select one</option>
   @foreach (var mod in values)
   {
        <option value="@mod.Id">@mod.Name</option>
   }
</select>

The class named Items that is populating the list:

public class Items
{
    public string Name { get; set; }
    public int Id { get; set; }

    public Items(string name, int id)
    {
        Name = name;
        Id = id;
    }
}

The list itself (before it is populated):

public List<Items> itm = new List<Items>();

The function that's called onchange:

public string SelectedValue = "";

public void selectedValue(ChangeEventArgs selectEvent)
{
    SelectedValue = selectEvent.Value.ToString();
}

So to sum it up, this is what's happening:

  1. The list is populated with Items
  2. The select list is built of the items in the list using a @foreach loop (see Razor code).
  3. An item is selected from the dropdown, this runs the selectedValue() function and changes the value and the SelectedValue string
  4. The selected item is deleted from the list of items
  5. The dropdown is rebuilt using the modified list
  6. The selected item is now set to the one directly below the deleted item, this is happening without onchange being run. This is the problem
  7. Now the selected value of the dropdown don't correspond to the one of the SelectedValue string.

This can probably be solved by setting the <select> element to its default option/value, but i can't figure out how to do this.

How can I change the selected value to be the default option (in my case the "select one" option with value "") of my dropdown list?


Solution

  • How can I change the selected value to be the default option (in my case the "select one" option with value "") of my dropdown list?

    I'm afraid you can do it only with JSInterop as follows:

    Generally speaking, the selectedIndex property on the select element is set to 0 after you select an option in the select element. This is done in the selectedValue method...

    This is a complete working code snippet. Run it and test it...

    @page "/"
    
    <select @ref="myselect" id="myselect" class="form-control"
            @onchange="selectedValue">
        <option selected value="-1">select one</option>
        @foreach (var item in items)
        {
            <option value="@item.ID">@item.Name</option>
        }
    </select>
    
    <p>@SelectedValue</p>
    
    @code {
        [Inject] IJSRuntime JSRuntime { get; set; }
        private ElementReference myselect;
    
        List<Item> items = Enumerable.Range(1, 10).Select(i => new Item { 
            Name = $"Name {i.ToString()}", ID = i }).ToList();
    
        public string SelectedValue = "";
    
        public void selectedValue(ChangeEventArgs args)
        {
            SelectedValue = args.Value.ToString();
            var item = items.Single(item => item.ID == Convert.ToInt32(SelectedValue));
            items.Remove(item);
    
            JSRuntime.InvokeVoidAsync("exampleJsFunctions.selectElement", myselect);
        }
    
        public class Item
        {
            public string Name { get; set; }
            public int ID { get; set; }
        }
    }
    
    

    Put the following JS code in the _Host.cshtml file:

    <script src="_framework/blazor.server.js"></script>
    <script>
        window.exampleJsFunctions =
        {
            selectElement: function (element) {
                element.selectedIndex = 0;
            }
        };
    </script>