How do you pre-select an item in a DropDownList? When a user has selected an item in a DropDownList we save that so that next time the list is displayed the previously selected item is shown. However, we can't find a way to do that.
Our DropDownList component is defined as:
<SfDropDownList DataSource="@businessGroups" TItem="BusinessGroup" TValue="int?" Placeholder="Select a Business Group" @bind-Value="@selectedGroupId">
<DropDownListFieldSettings Text="Name" Value="Id"></DropDownListFieldSettings>
</SfDropDownList>
The DataSource is defined as (the code to populate the businessGroups List is not shown):
private List<BusinessGroup> businessGroups = new List<BusinessGroup>();
internal class BusinessGroup
{
public BusinessGroup(int id, string name)
{
this.Id = id;
this.Name = name;
}
public int Id { get; private set; }
public string Name { get; private set; }
}
We expect to be able to set the initial Item displayed by the DropDownList with a statement like:
public int? selectedGroupId = 9;
where the 9, in this example, is a BusinessGroup.Id value in the businessGroups List. However, no matter what we try, the DropDownList stubbornly only displays the "Select a Business Group" Placeholder and not the businessGroup List entry with BusinessGroup.Id = 9.
Your advice would be appreciated.
Peter,
I think that the problem I've got is that my businessGroup List assigned to the DataSource Parameter and the setting of the bound selectedGroupId value happen after the SfDropDownList is rendered. They occur in response to a button click on the form that hosts the SfDropDownList component. Is there a way I can cause the SfDropDownList to refresh its parameters?