DropDown options displayed as int when using Data of type Enum #746

Closed
opened 2026-01-29 17:42:41 +00:00 by claunia · 5 comments
Owner

Originally created by @exrezzo on GitHub (Feb 23, 2023).

Using a DropDown populated with Data of type Enum leads to the condition that the possible options in the dropdown are displayed with their underlying int value.

image

I did some investigation (my first time I'm getting my hands dirty a bit within an open source project) and I've noticed that I can get some improvement by changing line 136 in DropDownbase.cs from:

 foreach (var item in LoadData.HasDelegate ? Data : View)
{
    RenderItem(builder, item);
}

to

foreach (var item in Data)
{
    RenderItem(builder, item);
}

what I've noticed is that while the Data list keeps reference to objects of the type Enum the View object is not doing it.
At this point View references a list of numbers, so essentially the displayable info is lost.
In fact, with this dummy edit I can get displayed correctly the string values
image

because it is handled by the method public object GetItemOrValueFromProperty(object item, string property) in DropDownBase.cs at line 442 with the EnumExtensions.GetDisplayDescription(enumValue)

even if this could be a way toward a solution, after setting the value I get a runtime error specified cast is not valid at line 1103 on method

await ValueChanged.InvokeAsync(object.Equals(internalValue, null) ? default(T) : (T)internalValue);

because the internalValue is now a string, and maybe it cannot cast back to the Enum type given that maybe it expects an int.

Given those evidences, do you think that something can be done?
Could I be useful in some way?

Thank you all for this wonderful library!

Originally created by @exrezzo on GitHub (Feb 23, 2023). <!-- IMPORTANT: Read this first!!! 1. If you own a Radzen Professional or Еnterprise subscription you can report your issue or ask us a question via email at info@radzen.com. Radzen staff will reply within 24 hours (Professional) or 16 hours (Enterprise) 2. The Radzen staff guarantees a response to issues in this repo only to paid subscribers. 3. If you have a HOW TO question start a new forum thread in the Radzen Community forum: https://forum.radzen.com. Radzen staff will close issues that are HOWTO questions. 4. Please adhere to the issue template. Specify all the steps required to reproduce the issue or link a project which reproduces it easily (without requiring extra steps such as restoring a database). --> Using a DropDown populated with Data of type Enum leads to the condition that the possible options in the dropdown are displayed with their underlying int value. ![image](https://user-images.githubusercontent.com/8734647/220972464-7348c6d6-fff7-415c-9b71-a6f80258f807.png) I did some investigation (my first time I'm getting my hands dirty a bit within an open source project) and I've noticed that I can get some improvement by changing line [136 ](https://github.com/radzenhq/radzen-blazor/blob/master/Radzen.Blazor/DropDownBase.cs#L135) in DropDownbase.cs from: ``` foreach (var item in LoadData.HasDelegate ? Data : View) { RenderItem(builder, item); } ``` to ``` foreach (var item in Data) { RenderItem(builder, item); } ``` what I've noticed is that while the Data list keeps reference to objects of the type Enum the View object is not doing it. At this point View references a list of numbers, so essentially the displayable info is lost. In fact, with this dummy edit I can get displayed correctly the string values ![image](https://user-images.githubusercontent.com/8734647/220975051-092f983c-8413-451e-896d-119e98676c52.png) because it is handled by the method `public object GetItemOrValueFromProperty(object item, string property)` in DropDownBase.cs at line [442 ](https://github.com/radzenhq/radzen-blazor/blob/master/Radzen.Blazor/DropDownBase.cs#L437) with the `EnumExtensions.GetDisplayDescription(enumValue)` even if this could be a way toward a solution, after setting the value I get a runtime error `specified cast is not valid` at line [1103 ](https://github.com/radzenhq/radzen-blazor/blob/master/Radzen.Blazor/DropDownBase.cs#L1097) on method ``` await ValueChanged.InvokeAsync(object.Equals(internalValue, null) ? default(T) : (T)internalValue); ``` because the internalValue is now a string, and maybe it cannot cast back to the Enum type given that maybe it expects an int. Given those evidences, do you think that something can be done? Could I be useful in some way? Thank you all for this wonderful library!
Author
Owner

@enchev commented on GitHub (Feb 24, 2023):

Hi @exrezzo,

You have this in WASM, right? You can cast to the enum type to avoid this. For example:

<RadzenDropDown @bind-Value=@tickPosition Data=@tickPositions  />
@code {
    IEnumerable<GaugeTickPosition> tickPositions = Enum.GetValues(typeof(GaugeTickPosition)).Cast<GaugeTickPosition>();
    GaugeTickPosition tickPosition = GaugeTickPosition.Outside;
}
@enchev commented on GitHub (Feb 24, 2023): Hi @exrezzo, You have this in WASM, right? You can cast to the enum type to avoid this. For example: ``` <RadzenDropDown @bind-Value=@tickPosition Data=@tickPositions /> @code { IEnumerable<GaugeTickPosition> tickPositions = Enum.GetValues(typeof(GaugeTickPosition)).Cast<GaugeTickPosition>(); GaugeTickPosition tickPosition = GaugeTickPosition.Outside; } ```
Author
Owner

@exrezzo commented on GitHub (Feb 24, 2023):

Yes @enchev , I'm using WASM.
Unfortunately your snippet still shows the problem.
To make sure there's no contamination in the test i created a single component with your code and the options are still shown as integers.
I used the package versione 4.7.2

@exrezzo commented on GitHub (Feb 24, 2023): Yes @enchev , I'm using WASM. Unfortunately your snippet still shows the problem. To make sure there's no contamination in the test i created a single component with your code and the options are still shown as integers. I used the package versione 4.7.2
Author
Owner

@enchev commented on GitHub (Feb 24, 2023):

Can you provide some sample code where this can be reproduced?

@enchev commented on GitHub (Feb 24, 2023): Can you provide some sample code where this can be reproduced?
Author
Owner

@exrezzo commented on GitHub (Feb 24, 2023):

I used the example you provided.

Given a Blazor WASM component like:

@page "/test"
<h3>TestComponent</h3>
<RadzenDropDown @bind-Value=@tickPosition Data=@tickPositions />
@code {
    IEnumerable<GaugeTickPosition> tickPositions = Enum.GetValues(typeof(GaugeTickPosition)).Cast<GaugeTickPosition>();
    GaugeTickPosition tickPosition = GaugeTickPosition.Outside;
}

you can see the error.
I'm using .NET 7, with the following libraries (Radzen version: 4.7.2):

<ItemGroup>
    <PackageReference Include="Bogus" Version="34.0.2" />
    <PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.2" PrivateAssets="all" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
    <PackageReference Include="Radzen.Blazor" Version="4.7.2" />
    <PackageReference Include="Texnomic.Blazor.JsonViewer" Version="0.1.0" />
  </ItemGroup>
@exrezzo commented on GitHub (Feb 24, 2023): I used the example you provided. Given a Blazor WASM component like: ``` @page "/test" <h3>TestComponent</h3> <RadzenDropDown @bind-Value=@tickPosition Data=@tickPositions /> @code { IEnumerable<GaugeTickPosition> tickPositions = Enum.GetValues(typeof(GaugeTickPosition)).Cast<GaugeTickPosition>(); GaugeTickPosition tickPosition = GaugeTickPosition.Outside; } ``` you can see the error. I'm using .NET 7, with the following libraries (Radzen version: 4.7.2): ``` <ItemGroup> <PackageReference Include="Bogus" Version="34.0.2" /> <PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.2" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.2" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.2" PrivateAssets="all" /> <PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" /> <PackageReference Include="Radzen.Blazor" Version="4.7.2" /> <PackageReference Include="Texnomic.Blazor.JsonViewer" Version="0.1.0" /> </ItemGroup> ```
Author
Owner

@exrezzo commented on GitHub (Feb 24, 2023):

Omg I found what was the problem.
Indeed, I didn't explore deeply enough your examples showcase, in particular the one dedicated to DropDown.

Here the example you give needs to change from
IEnumerable<GaugeTickPosition> tickPositions = Enum.GetValues(typeof(GaugeTickPosition)).Cast<GaugeTickPosition>();
to
IEnumerable<Enum> tickPositions = Enum.GetValues(typeof(GaugeTickPosition)).Cast<Enum>();

where the cast has to be to the Enum type and not the particular GaugeTickPosition enum type.

Now it works like charm, so this issue was a false alarm.
Thanks a lor @enchev for your time, sorry if I made you waste your time.

Keep rockin

@exrezzo commented on GitHub (Feb 24, 2023): Omg I found what was the problem. Indeed, I didn't explore deeply enough your examples showcase, in particular the one dedicated to DropDown. Here the example you give needs to change from `IEnumerable<GaugeTickPosition> tickPositions = Enum.GetValues(typeof(GaugeTickPosition)).Cast<GaugeTickPosition>();` to `IEnumerable<Enum> tickPositions = Enum.GetValues(typeof(GaugeTickPosition)).Cast<Enum>();` where the cast has to be to the `Enum` type and not the particular `GaugeTickPosition` enum type. Now it works like charm, so this issue was a false alarm. Thanks a lor @enchev for your time, sorry if I made you waste your time. Keep rockin ✨
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#746