Set Item in DropDownList

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,


3 Replies

BC Berly Christopher Syncfusion Team September 9, 2021 12:40 PM UTC

Hi Peter, 
  
Greetings from Syncfusion support. 
  
We have checked the DropDownList component with shared sample. Unfortunately, the reported issue does not occurred at our end. So, we have prepared the sample and attached it below. 
  
  
Screenshot: 
  
 
  
So, please check the attached sample and modify the attached sample with reported issue else simple issue reproducing sample along with Syncfusion NuGet version that will help us to check and proceed further from our end. 
  
Regards, 
Berly B.C 



PE Peter September 9, 2021 08:22 PM UTC

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?



BC Berly Christopher Syncfusion Team September 10, 2021 12:35 PM UTC

Hi Peter, 
  
We have checked the DropDownList component by setting the component value dynamically. But the reported issue does not occurred at our end. We have modified the sample and attached it below. Also, we suggest you to call StateHasChanged() method to refresh the component in the Blazor application.  
  
  
Still issue persists, please share the issue reproducing sample or modify the attached sample that will help us to check and proceed further from our end. 
  
Regards, 
Berly B.C 


Loader.
Up arrow icon