I want to create a drop down list with months that will return a list of MonthEnum but it always returns null. It works with everything else except enums.
This is the code:
public class EnumDefinition<T> where T: Enum {
public string Name { get; set; }
public T Value { get; set; }
}
public static class EnumExtensions
{
internal static ResourceManager oResourceManager = new ResourceManager("Imagin.Shared.Resources.Resource", typeof(Resource).Assembly);
private static CultureInfo CurrentCulture { get => CultureInfo.CurrentCulture; }
public static EnumDefinition[] GetDefinition
Hi Giannis,
Find the dropdownlist component with Enum data in the below code example.
find the code example here:
[enumwrapper.cs] public class EnumWrapper<TEnum> where TEnum : struct, Enum { public TEnum Value { get; set; } public string DisplayText { get; set; }
public EnumWrapper(TEnum value, string? displayText) { Value = value; DisplayText = (displayText ?? value.ToString())!; }
public static List<EnumWrapper<TEnum>> Build() => Enum.GetValues<TEnum>() .Select(e => new EnumWrapper<TEnum>(e, e.ToString())) .OrderBy(w => w.DisplayText) .ToList(); }
[countryEnum.cs]
public enum CountryEnum { Belgium, Netherlands, France, Germany }
[sample.razor]
<SfDropDownList TValue="CountryEnum" TItem="EnumWrapper<CountryEnum>" DataSource="EnumWrapper<CountryEnum>.Build()" FloatLabelType="Syncfusion.Blazor.Inputs.FloatLabelType.Auto" Placeholder="Placeholder" @bind-Value="country2"> <DropDownListFieldSettings Text="@nameof(EnumWrapper<CountryEnum>.DisplayText)" Value="@nameof(EnumWrapper<CountryEnum>.Value)" /> </SfDropDownList>
@code { public CountryEnum DropVal { get; set; } = CountryEnum.Germany;
private IEnumerable<EnumWrapper<CountryEnum>> data { get; set; } = Array.Empty<EnumWrapper<CountryEnum>>(); protected override void OnInitialized() { data = EnumWrapper<CountryEnum>.Build(); } CountryEnum country2 { get; set; } = CountryEnum.Germany; } |
Find the sample in the attachment:
Note: If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Regards,
Sureshkumar P
I'm sorry if it wasn't clear but i want a MultiSelect that returns a List of Enums.
Thank you.
As per the request, we have modified the sample from dropdownlist into multiselect component.
Find the code example here:
<SfMultiSelect TValue="CountryEnum[]" TItem="EnumWrapper<CountryEnum>" DataSource="EnumWrapper<CountryEnum>.Build()" FloatLabelType="Syncfusion.Blazor.Inputs.FloatLabelType.Auto" Placeholder="Placeholder" @bind-Value="DropVal"> <MultiSelectFieldSettings Text="@nameof(EnumWrapper<CountryEnum>.DisplayText)" Value="@nameof(EnumWrapper<CountryEnum>.Value)" /> </SfMultiSelect>
@code {
private IEnumerable<EnumWrapper<CountryEnum>> data { get; set; } = Array.Empty<EnumWrapper<CountryEnum>>(); protected override void OnInitialized() { data = EnumWrapper<CountryEnum>.Build(); } public CountryEnum[] DropVal { get; set; } = new CountryEnum[] { CountryEnum.Germany }; } |
Find the modified sample in the attachment:
I tried your example exactly and when i choose a second value it returns null and then the binding is off.
If it helps it works without the EnumWrapper.
You can resolve your faced issue by changing the TValue type to string array instead of Enum array.
Find the code example here:
@if (DropVal !=null) {
@foreach (var SelectedValue in DropVal) { <p>MultiSelect value is:<strong>@SelectedValue</strong></p> } }
<SfMultiSelect TValue="string[]" TItem="EnumWrapper<CountryEnum>" DataSource="data" FloatLabelType="Syncfusion.Blazor.Inputs.FloatLabelType.Auto" Placeholder="Placeholder" @bind-Value="DropVal"> <MultiSelectFieldSettings Text="@nameof(EnumWrapper<CountryEnum>.DisplayText)" Value="@nameof(EnumWrapper<CountryEnum>.Value)" /> </SfMultiSelect>
@code {
private IEnumerable<EnumWrapper<CountryEnum>> data { get; set; } = Array.Empty<EnumWrapper<CountryEnum>>(); protected override void OnInitialized() { data = EnumWrapper<CountryEnum>.Build(); } public string[] DropVal { get; set; } = new string[] { }; } |
Find the modified sample output screenshot here:
|
Find the modified sample in the attachment:
My problem is that i want to return enum values. Not strings not integers either. For now i have found a solution by returning int array and translate it to enum array.
I want to know if there is a way more straightforward.
Thank you.
Giannis, when using multiselect component the value property needs to handle multiple values so the enum value does not support multiple values handled. So, we suggest you achieve your requirement by using a string array or list of string in the TValue attribute as in our previous update.