We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Update DropDownList Items dynamically

Hi, I can't update DropDownList Items dynamically, is there any way it can be accomplished?


Thanks

3 Replies

SN Sevvandhi Nagulan Syncfusion Team August 1, 2019 10:19 AM UTC

Hi Oswaldo, 
  
Greetings from Syncfusion support.  

Yes, it can be accomplished by using our public method AddItem. We Added the new items in DropDownList items by clicking the button as mentioned below, 

[Index.razor] 
   <div> 
    <EjsDropDownList @ref="@DropDownObj" Placeholder="Select a game" DataSource="@LocalData"> 
        <DropDownListFieldSettings Value="ID" Text="Text"></DropDownListFieldSettings> 
    </EjsDropDownList> 
</div> 
<div> 
    <input type="button" value="AddItem" onclick="@AddItems" /> 
</div> 
 
@code { 
    EjsDropDownList DropDownObj; 
    public class Games 
    { 
        public string ID { get; set; } 
        public string Text { get; set; } 
    } 
    List<Games> LocalData = new List<Games> { 
    new Games() { ID= "Game1", Text= "American Football" }, 
    new Games() { ID= "Game2", Text= "Badminton" }, 
    new Games() { ID= "Game3", Text= "Basketball" }, 
    new Games() { ID= "Game4", Text= "Cricket" } 
  }; 
    public List<Games> Item = new List<Games>{ 
    new Games() { ID= "Game6", Text= "Golf" } 
   }; 
public void AddItems() 
{ 
    this.DropDownObj.AddItem(Item); 
} 
} 

  
Note: We're working on giving Observable collection DataSource support in blazor, so once it's rolled out we can add the item directly into DataSource(It will be included in Today’s Release). 
  
Please find the sample below, 


Regards, 
Sevvandhi N 



OR Oswaldo Raul Diaz Guzman August 1, 2019 04:12 PM UTC

Thanks so much, I've been using and recomending Synfusion for years an I'm very happy working with Syncfusion's blazor components.




SN Sevvandhi Nagulan Syncfusion Team August 2, 2019 02:01 PM UTC

Hi Oswaldo, 

Most welcome. 
  
We are glad to announce that our latest patch release in the version 17.2.0.40-beta has been rolled out successfully and in that release, we have included the “Observable collection data binding support”. 
 
Import the ObjectModel from collections and change the List type to ObservableCollection type. You can then add the items directly to DataSource as shown below.  
 
[Index.razor] 
@using System.Collections.ObjectModel; 
<div> 
    <EjsDropDownList Placeholder="Select a game" DataSource="@LocalData" TValue="string"> 
        <DropDownListFieldSettings Value="ID" Text="Text"></DropDownListFieldSettings> 
    </EjsDropDownList> 
</div> 
<div> 
    <input type="button" value="AddItem" onclick="@AddItems" /> 
</div> 
 
@code {  
    public class Games 
    { 
        public string ID { get; set; } 
        public string Text { get; set; } 
    } 
    ObservableCollection<Games> LocalData = new ObservableCollection<Games> { 
    new Games() { ID= "Game1", Text= "American Football" }, 
    new Games() { ID= "Game2", Text= "Badminton" }, 
    new Games() { ID= "Game3", Text= "Basketball" }, 
    new Games() { ID= "Game4", Text= "Cricket" } 
  }; 
     
public void AddItems() 
{ 
    this.LocalData.Add(new Games() { ID= "Game6", Text= "Golf" }); 
} 
} 


Please find the NuGet from the below link. 



Please find the release notes details from the below link. 


Please find the sample below, 

 

Regards, 
Sevvandhi N 


Loader.
Live Chat Icon For mobile
Up arrow icon