DropDown (multiple selection) does not work right for Enums #357

Closed
opened 2026-01-29 17:35:59 +00:00 by claunia · 1 comment
Owner

Originally created by @rojait00 on GitHub (Apr 4, 2022).

Hi,
I'm currently working on a website that lets you execute Regex.Replace(...).
Therefore, I wanted to create a DropDown component (with multiple selection) containing the diferent RegexOptions(Enum).
So, I added the following Razor snippet:

<RadzenDropDown AllowClear="true" AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                        Multiple="true" Placeholder="Select Regex Options..." 
                        @bind-Value=@selectedOptions  Data=@regexOptions />

The variables are defined like that:

@code
{
    IEnumerable<RegexOptions> regexOptions = (RegexOptions[])Enum.GetValues(typeof(RegexOptions));
    IEnumerable<RegexOptions> selectedOptions = new List<RegexOptions>() { RegexOptions.IgnoreCase };
}

Describe the bug
At first it looked perfect. The string representation of the default option is displayed:
image

But if you open the DropDown it displays the numeric values of each option:

image

So one time it uses the internal value and in the other case it uses the .ToString() value. This is not consistent.
This inconsistency results in a broken selection.
In this example "MultiLine"(=2) was selected, but the checkbox is not checked.
image

Here you can find the complete view:
https://github.com/rojait00/RegexReplacer/blob/0d15aaf0b0caf73df35e51392306ba7d7425a925/RegexReplacer/Client/Pages/Index.razor

Expected behavior
My expectation would be that it would use
A: only the values or
B: only the string representation.

In case of an Enum preferably the string representation.

Desktop

  • OS: Win10
  • Browser Chrome
  • Version 99
  • Radzen.Blazor 3.17.2
Originally created by @rojait00 on GitHub (Apr 4, 2022). Hi, I'm currently working on a website that lets you execute `Regex.Replace(...)`. Therefore, I wanted to create a DropDown component (with multiple selection) containing the diferent `RegexOptions`(Enum). So, I added the following Razor snippet: ``` <RadzenDropDown AllowClear="true" AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" Multiple="true" Placeholder="Select Regex Options..." @bind-Value=@selectedOptions Data=@regexOptions /> ``` The variables are defined like that: ``` @code { IEnumerable<RegexOptions> regexOptions = (RegexOptions[])Enum.GetValues(typeof(RegexOptions)); IEnumerable<RegexOptions> selectedOptions = new List<RegexOptions>() { RegexOptions.IgnoreCase }; } ``` **Describe the bug** At first it looked perfect. The string representation of the default option is displayed: ![image](https://user-images.githubusercontent.com/17075029/161256689-aab01c11-dab5-4614-a9d7-d4055d0bdef5.png) But if you open the DropDown it displays the numeric values of each option: ![image](https://user-images.githubusercontent.com/17075029/161256844-f9732670-2c67-47c2-be6c-e3e39dd11e9c.png) So one time it uses the internal value and in the other case it uses the `.ToString()` value. This is not consistent. This inconsistency results in a broken selection. In this example "MultiLine"(=2) was selected, but the checkbox is not checked. ![image](https://user-images.githubusercontent.com/17075029/161257367-cd088c6a-b824-4de6-b986-a4456dfaf93d.png) Here you can find the complete view: [https://github.com/rojait00/RegexReplacer/blob/0d15aaf0b0caf73df35e51392306ba7d7425a925/RegexReplacer/Client/Pages/Index.razor](https://github.com/rojait00/RegexReplacer/blob/0d15aaf0b0caf73df35e51392306ba7d7425a925/RegexReplacer/Client/Pages/Index.razor) **Expected behavior** My expectation would be that it would use A: only the values or B: only the string representation. In case of an Enum preferably the string representation. **Desktop** - OS: Win10 - Browser Chrome - Version 99 - Radzen.Blazor 3.17.2
Author
Owner

@rojait00 commented on GitHub (Apr 4, 2022):

(I updated to Radzen.Blazor 3.17.3)

I tried to develop a workaround by using a class with the properties Name and Value.
I used LINQ to generate an IEnumerable containing objects representing each option and found an even more confusing behavior.
So I started using handwritten Lists with values. They worked great.
That brought me to the conclusion that you cannot assign every IEnumerable to the Data property. It MUST be a List!
RegexOptions[] also does not work!

My new Code looks like this:

<RadzenDropDown AllowClear="true" AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                Placeholder="Select Option..." Multiple="true" Class="w-50"
                @bind-Value=@selectedOptions Data=@regexOptions />

@code
{
    IEnumerable<RegexOptions> regexOptions = GetOptions();
    IEnumerable<RegexOptions> selectedOptions = new[] { RegexOptions.IgnoreCase };

    private static List<RegexOptions> GetOptions()
    {
        var values = (RegexOptions[])Enum.GetValues(typeof(RegexOptions));
        return values.ToList();
    }
}

Because it does not seem to be a problem with enums I will close this issue and open a new one.

@rojait00 commented on GitHub (Apr 4, 2022): (I updated to Radzen.Blazor 3.17.3) I tried to develop a workaround by using a class with the properties Name and Value. I used `LINQ` to generate an `IEnumerable `containing objects representing each option and found an even more confusing behavior. So I started using handwritten Lists with values. They worked great. That brought me to the conclusion that you cannot assign every `IEnumerable` to the `Data` property. It MUST be a `List`! `RegexOptions[]` also does not work! My new Code looks like this: ``` <RadzenDropDown AllowClear="true" AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" Placeholder="Select Option..." Multiple="true" Class="w-50" @bind-Value=@selectedOptions Data=@regexOptions /> @code { IEnumerable<RegexOptions> regexOptions = GetOptions(); IEnumerable<RegexOptions> selectedOptions = new[] { RegexOptions.IgnoreCase }; private static List<RegexOptions> GetOptions() { var values = (RegexOptions[])Enum.GetValues(typeof(RegexOptions)); return values.ToList(); } } ``` Because it does not seem to be a problem with enums I will close this issue and open a new one.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#357