6

I am dynamicaly 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 whitout 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 code, 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 populationg 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 thats 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 pupolated with Items.

  2. the select list is built of the items in the list using a @foreach loop (se razor code).

  3. an item is selected from the dropdown, this runs the selectedValue() function and changes the value ud the SelectedValue string.

  4. the selected item is deleted from the list of items

  5. the dropdown is rebuilt using the modefied 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 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?

user10483669
  • 548
  • 2
  • 6
  • 17

3 Answers3

7

I was having a similar issue. I solved it by putting an if statement in the loop that generates the option elements and from there conditionally add the selected attribute, i.e.;

<select @onchange="listChanged">
    @foreach (var item in PageModel.Lists)
    {
        if(item.Id == currListId)
        {
            <option value="@item.Id" selected>@item.Name</option>
        }
        else
        {
            <option value="@item.Id">@item.Name</option>
        }
    }
</select>

Sorry for not taking the time to adapt my code to your situation, but you've already solved your problem. This is for anyone else that comes across this problem (prolly me next time i need to do something like this, lol).

MikeT
  • 2,530
  • 25
  • 36
5

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>
codersl
  • 2,222
  • 4
  • 30
  • 33
enet
  • 41,195
  • 5
  • 76
  • 113
  • I figured that there where a way of doing this using JSInterop but tought that there might be a better way that im just missing. Its sad that there isn't. Im accepting your answer, thanks for the help! – user10483669 Mar 10 '20 at 08:09
  • @user10483669 Yes, it is possible using only blazor. You just need to code everything using html+css+blazor. And do not use select-option structure. Just use

    , tags, you need a drop-down pop-up, and some events handled by blazor.

    – Sorush Mar 11 '20 at 12:49
  • @Sorush rly? thats gr8th, care to make a example? have not seen any around and diden't quite get it to work on my own. – user10483669 Mar 12 '20 at 14:30
  • This is a real disappointment. How else would I want to set the selected values? Really not impressed with MudBlazor Select lists so far. – Zapnologica Dec 22 '21 at 06:26
0

A similar problem confronted me in a .NET MAUI Blazor project. In the project I'm working on a view model manages most of the form behavior. In addition, CommunityToolkit.Mvvm is used to manage the property changed management. So this answer will base itself on that configuration adjusted for using bindings. It also follows answer given by @MikeT.
The assumption is that another element is effecting the selection decision. In my case if there is only one element in values to choose from, that element would become selected automatically. Otherwise, the user makes a selection.

    <select class="form-control" @Bind="vm.selectedValueId"
       disabled="@(vm.IsValuesDataLoaded == false)">
       <option value="">select one</option>
       @foreach (var mod in values)
         {
           @if(@mod.Id == @vm.selectedValueId)
           {
              <option value="@mod.Id" selected>@mod.Name</option>
           }
           else
           {
              <option value="@mod.Id">@mod.Name</option>
           }
         }
    </select>

@code{
   ValueViewModel vm;  //instantiated inside OnInitialized
   // more ...
}

As the view model is handling construction of the element list in values, all of the activity takes place there. So this section of view model code would be --

partial void OnSomeOtherPreliminaryPropertyChanged(string? value)
{
    //  code for processing this event
    //  condition test could also be for an entry previously selected
    if(values.Count == 1)
    {
        SelectedValueId = whatever_Value_Should_Be_Default_For_Selection;
        OnModuleIdChanged(selectedValueId);
    }
    IsValuesDataLoaded = true;
}

[ObservableProperty]
private int selectedValueId;
[ObservableProperty]
private string selectedValue;

//OnSelected... is called when selectedValueId changes. In this case
// the code is changing the selection
partial void OnSelectedValueIdChanged(int? value)
{
    SelectedValue = Values[value].Name
}

public bool IsValuesDataLoaded  //controls the enabled state of the select element
{
    get;
    private set;
} = false;