Can't get RadzenDropDown to bind to a list of strings 'Specified cast is not valid.'

I'm getting the following error with the dropdown control. It loads fine, populates the list fine, the initial values from the data are selected. But if I try to change the selection, I get the following:

crit: 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.<SelectItem>d__100[[System.Collections.Generic.List1[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()

The data 'roles' is a List of string, the thing I'm trying to bind to 'editUser.roles' is also a List of string. I don't get what it's trying to cast to throw this error.

<RadzenDropDown
id="roles"
Class="form-control"
@bind-Value=@editUser.Roles
Multiple="true"
SelectAllText="Select All"
Placeholder="Select..."
Data="@roles" />

This is exactly how the DropDown is bound in a sample app with security generated by Radzen:



You can check it for reference.

Where do I find that example? What is '@roles' there, is it a list of strings? What type is 'user.roleNames'?

I'm not using your code generator thing, just the component library in visual studio.

You can generate it with our generator thing.

It's IEnumerable<string>

1 Like

Is what I'm pointing out a bug in the RadzenDropDown control, or am I missing something about how it's meant to work? Can it work with lists or does it specifically need IEnumerable?

I can't change it from List of Role to IEnumerable of Role, because that breaks the EF mapping on the database, so I don't know whether that would fix it.

It needs specifically IEnumerable<>.

Usually EF mapping works normally with IEnumerable<> - this is how actually Radzen EF context and models are generated.

This doesn't work with entity framework 6 and Azure Cosmos Db. This is the error you get if you try that:

System.InvalidOperationException: 'The property 'ApplicationUser.Roles' could not be mapped because it is of type 'IEnumerable', which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'

Where as it works perfectly fine as a list.

Is it within the realms of possibility in future versions of Radzen that dropdown could be a bit more flexible in what it'll bind to? A list implements IEnumerable after all.

We accept pull requests!

UPDATE: Actually I've made support for this case - it will be available with our update before the end of the week:

1 Like

Amazing, thank you, that's great. In the mean time I've worked around it with a non-mapped property on my model that sets the mapped one and then bound to that on the dropdown control e.g.:

public List<string> Roles { get; set; } = new List<string>();

[NotMapped]
public IEnumerable<string> RolesEnumerable
{
    get
    {
        return this.Roles;
    }
    set
    {
        this.Roles = value.ToList();
    }
}

That lets me work around it for now.

I'll hopefully try the updated version soon (though my partner is due to give birth any day now, so might be a while!)