DropdownList Enum template ?

Hi,

Syncfusion.Blazor 18.4.0.49

We are trying the template with Dropdown List and it works well in scenarios like this:
<SfDropDownList TValue="string" TItem="EmployeeData" Placeholder="Select a customer" Query="@Query">
    <DropDownListTemplates TItem="EmployeeData">
        <ItemTemplate>
            <span><span class='name'>@((context as EmployeeData).FirstName)</span><span class='country'>@((context as EmployeeData).Country)</span></span>
        </ItemTemplate>
    </DropDownListTemplates>
    <SfDataManager Url="https://ej2services.syncfusion.com/production/web-services/api/Employees" Adaptor="Syncfusion.Blazor.Adaptors.WebApiAdaptor" CrossDomain="true"></SfDataManager>
    <DropDownListFieldSettings Text="FirstName" Value="Country"></DropDownListFieldSettings>
</SfDropDownList>
But how do we make it work with Enums? Nothing is happening when ItemTemplate is used here:

<SfDropDownList TValue="Values" TItem="string" Placeholder="e.g. Australia" DataSource="@EnumValues" @bind-Value="@ddlVal">
<DropDownListTemplates TItem="string">
        <ItemTemplate context="country">
            <span>Hello @country</span>
        </ItemTemplate>
</DropDownListTemplates>
</SfDropDownList>

8 Replies

BC Berly Christopher Syncfusion Team May 12, 2021 12:43 PM UTC

Hi Jesper, 
  
Greetings from Syncfusion support. 
  
Yes, DropDownList component will be working fine with ENUM and item template in the mentioned product version 18.4.0.49. For your convenience, we have prepared the sample and attached it below. 
  
  
Screenshot: 
  
 
  
Please check the attached sample and share any issue reproducing sample or faced issue screenshot that will help us to check and proceed further from our end. 
  
Regards, 
Berly B.C 
  



DA David July 14, 2021 02:30 AM UTC

So the solution is to define the list of values as the name​ of the enum value, and not the enum itself.

This is a poor solution but at least it will work. Why can't we bind to the typed enum value direct though? 



BC Berly Christopher Syncfusion Team July 14, 2021 04:01 PM UTC

Hi David, 
  
We will validate and update the further details in two business days (16th July 2021). We appreciate your patience until then. 
  
Regards, 
Berly B.C 



BC Berly Christopher Syncfusion Team July 15, 2021 04:49 PM UTC

Hi Jesper, 
  
Thanks for the patience. 
  
We can assign the Enum values directly to the DropDownList component. For your convenience, we have prepared the sample and attached it below. 
  
  
If we misunderstood your query, please share any code example of component rendering / Enum definition else modify the attached sample with reported issue that will help us to check and proceed further from our end. 
  
Regards, 
Berly B.C 



DA Daniel September 23, 2021 06:08 PM UTC

It's is possible to user DropDownList with enums and a TEMPLATE


follow the example:

declare the DataSource:



public List<TipoTemplateEmail> TiposTemplate = Enum.GetValues(typeof(TipoTemplateEmail)).Cast<TipoTemplateEmail>().ToList();


         <SfDropDownList TItem="TipoTemplateEmail" TValue="TipoTemplateEmail?" PopupHeight="230px" Placeholder="Race"
             @bind-Value="@TemplateEmail.TipoTemplateEmail" DataSource="@TiposTemplate">              
            <DropDownListTemplates TItem="TipoTemplateEmail">
               <ItemTemplate>
                  @context.GetDisplayName()
               </ItemTemplate>
            </DropDownListTemplates>
         </SfDropDownList>

The GetDisplayName() is an extension method to Get the Display (name) of Enum 



BC Berly Christopher Syncfusion Team September 24, 2021 07:12 AM UTC

Hi Daniel, 
  
Thanks for the suggestion. Please let us know if you need further assistance on this. 
  
Regards, 
Berly B.C 



DA Daniel May 18, 2023 07:59 PM UTC

I'm posting another solution, as the past one (With Template) doesn't seems to work anymore

First create a List of KeyValuePair from the Enum:


private List<KeyValuePair<TipoTemplateEmail, string>> ListTiposTemplate { get; set; } =
   Enum.GetValues(typeof(TipoTemplateEmail)).Cast<TipoTemplateEmail>().ToList()
      .Select(s => new { Key = s, Value = s.GetDisplayName().ToString() })
      .ToDictionary(d => d.Key, d => d.Value).ToList();


them create your dropdown list as:

         <SfDropDownList TItem="KeyValuePair<TipoTemplateEmail, string>" TValue="TipoTemplateEmail?"

            @bind-Value="@TemplateEmail.TipoTemplateEmail" DataSource="@ListTiposTemplate">
            <DropDownListFieldSettings Value="Key" Text="Value"></DropDownListFieldSettings>
         </SfDropDownList>




UD UdhayaKumar Duraisamy Syncfusion Team May 20, 2023 11:44 AM UTC

Thank you for sharing the alternative solution. We appreciate your contribution. If you have any more questions or need further assistance, please let us know.


Loader.
Up arrow icon