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:
the list is pupolated with Items.
the select list is built of the items in the list using a @foreach loop (se razor code).
an item is selected from the dropdown, this runs the selectedValue() function and changes the value ud the SelectedValue string.
the selected item is deleted from the list of items
the dropdown is rebuilt using the modefied list
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
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?
, tags, you need a drop-down pop-up, and some events handled by blazor.
– Sorush Mar 11 '20 at 12:49