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

List of Enum Values DataBinding returns null

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() where TEnum: Enum

{

var type = typeof(TEnum);

return Enum.GetValues(type).Cast().Select(x => new EnumDefinition() {

Value = x,

Name = oResourceManager.GetString(type.GetMember(x.ToString()).First().GetCustomAttribute()?.Name ?? "", CurrentCulture) ?? x.ToString(),

}).ToArray();

}

}

@Code{

MonthEnum[] Months;

}


I also tried with tuples as the documentation mentions but it is the same result Thanks in advance.


7 Replies

SP Sureshkumar P Syncfusion Team November 9, 2022 06:02 AM

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



Attachment: DDLServer_e919d1b5.zip


GI Giannis November 9, 2022 06:30 AM

I'm sorry if it wasn't clear but i want a MultiSelect that returns a List of Enums.

Thank you.



SP Sureshkumar P Syncfusion Team November 10, 2022 06:05 AM

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:


Attachment: DDLServer_8ca57ea7.zip


GI Giannis November 10, 2022 08:51 AM

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.



SP Sureshkumar P Syncfusion Team November 14, 2022 05:39 AM

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:


Attachment: DDLServer_969a2300.zip


GI Giannis replied to Sureshkumar P November 18, 2022 03:12 PM

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.



SP Sureshkumar P Syncfusion Team November 21, 2022 07:25 AM

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.


Loader.
Live Chat Icon For mobile
Up arrow icon