RadzenDropDown with Enum Cast not valid

Keep receiving the following error Cast not valid when switching values
OrderStyle is an enum with 3 values
public enum OrderStyle {Simple,Fast,Slow}

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Specified cast is not valid.
System.InvalidCastException: Specified cast is not valid.
at Radzen.DropDownBase1[T].SelectItem (System.Object item, System.Boolean raiseChange) <0x39f0980 + 0x0024a> in <filename unknown>:0 at Radzen.Blazor.RadzenDropDown1[TValue].OnSelectItem (System.Object item) <0x3dd46e0 + 0x001a2> in :0
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion (System.Threading.Tasks.Task task) <0x3dd5a90 + 0x000da> in :0

at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x3818ba0 + 0x000b6> in :0

<RadzenDropDown Data="Enum.GetNames(typeof(OrderStyle)).ToList()" @bind- 
Value="myOrder.OrderStyle" SelectedItem="OrderStyle.NEUTRAL" />

  @* @bind-Value="clientOrder.OrderStyle" SelectedItemChanged="(args=> ChangeOrderStyle(args))"

What's the correct approach for a drop down with enums, that you want associated to the new object (i.e. if you were creating a new order)

1 Like

Hi @iamthemarkus,

This demo shows a working usage of DropDowns and enums: https://blazor.radzen.com/styling-chart

The code is mostly like this:

<RadzenDropDown Data="@colorSchemes" @bind-Value="@colorScheme" />
@code {
    IEnumerable<ColorScheme> colorSchemes =  Enum.GetValues(typeof(ColorScheme)).Cast<ColorScheme>();
    ColorScheme colorScheme = ColorScheme.Palette;
}
2 Likes

I can see that this works in general and I've managed to correct my case.

One perhaps not expected behavior was that because my enum values have numbers associated to them
the method suggested will cause the number to be displayed in the drop, then rendered as the name representation once selected.

Bumping thread as I have the same issue.

When binding to an enum and the drop down list shows the Value instead of the Name of the enum.

I tried copying the example provided by @korchev and it shows the Values as well.

My enum...
protected Grid.Modes SelectedMode = Grid.Modes.Small;
protected IEnumerable<Grid.Modes> GameModes = Enum.GetValues(typeof(Grid.Modes)).Cast<Grid.Modes>();
public enum Modes { Small, Medium, Large }
<RadzenDropDown Data="GameModes" @bind-Value="SelectedMode" Style="margin-bottom: 20px" Change="GameModeChanged" />

2020-07-10 10_18_11-Minesweeper

Example screenshot in next post...

This is with version 2.10.15

Screenshot of the example...
2020-07-10 10_18_29-Minesweeper
Another note is that I am running in WASM and not Server Hosted.

1 Like

I guess Enum.GetValues works differently in server-side blazor and returns string values. Frankly I am not sure this is related to Radzen. What happens when you use a regular <select>?

I am also facing the same issue. How did you resolve it?

1 Like

this worked for me.

Data Model Class

public class MyStuff
{
    public enum MyTypes
    {
        bad,
        good,
        ugly
    }

    public MyTypes MyType { get; set; }

WASM

                                <div>
                                    <RadzenDropDown Data="@MyTypes" TextProperty="EnumName" ValueProperty="EnumValue" @bind-Value=MyStuff.MyType" style="width: 100%;" >
                                    </RadzenDropDown>
                                </div>

@code {
// define a class to support the dropdown
public class EnumMyTypes
{
public MyStuff.MyTypes EnumValue { get; set; }
public string EnumName { get; set; }
}
// list for the dropdown
List MyTypes { get; set; } = new List();

protected override void OnInitialized()
{
    try
    {
        // load the list

        foreach (MyStuff.MyTypes item in Enum.GetValues(typeof(MyType)))
        {
            MyTypes.Add(new EnumMyTypes { EnumName = item.ToString(), EnumValue = item });
        }

It shows the values unless you add this to your RadzenDropDown element:


<Template>
    @Enum.GetName(typeof(MyType), context)
</Template>