Specified cast is not valid in Blazor WebAssembly

I am getting 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) <0x4770518 + 0x0024a> in <filename unknown>:0 at Radzen.Blazor.RadzenDropDown1[TValue].OnSelectItem (System.Object item) <0x476f770 + 0x001a2> in :0

I am using Blazor WebAssembly(3.2) and using Radzen drop down inside editform

  <RadzenDropDown id="CountryName" AllowClear="true" TValue="string" Placeholder="Select..."
                                                        FilterOperator="StringFilterOperator.Contains" AllowFiltering="true"
                                                        Data="@countryList" TextProperty="Country_Name" ValueProperty="countryiD_PK"  @bind-Value="_registerModel.CountryName"                                                    />

Note: that countryiD_PK is a guid value.

I am trying to build cascading drop downs for Country,State & City.

bind-Value will attempt to set ValueProperty value to specified property. You cannot set GUID value to string property.

Well i corrected my table to appropriate GUID, but still getting the error, why now ?

                        <RadzenDropDown id="CountryName" AllowClear="true" TValue="Guid" Placeholder="Select..." Style="display:block"
                                        FilterOperator="StringFilterOperator.Contains" AllowFiltering="true"
                                        Data="@countryList" TextProperty="Country_Name" ValueProperty="CountryID_PK"
                                        SelectedItemChanged="@(obj => SelectedCountryChanged(obj))" />

has Country_Name(String) & CountryID_PK (GUID)

CountryID_FK (GUID)

Any insight into how to fix this ?
Can i do explicit cast using lambdas or other options to manually do it ?

I cannot tell why you get this exception from the info provided so far. If you have Radzen subscription you can send us your application to info@radzen.com.

At this time i am just exploring Radzen, don't have any subscription.

I just ran across the same issue, but no subscription for me either.

Mine are custom objects: Alliance & Power

private Alliance selectedAlliance;
private Power selectedPower;

private async Task CreateGameAlliance() => await dialogService.OpenAsync("New Game Alliance", ds =>
@<RadzenCard Style="padding: 20px;">
    <div class="row">
        <div class="col-md-12">
            <h3>Select a power to add to an alliance</h3>
                <RadzenDropDown @bind-Value="@selectedAlliance" AllowClear="true" Data="@alliances" TValue="Alliance" TextProperty="Name" ValueProperty="Id" />
                <RadzenDropDown @bind-Value="@selectedPower" AllowClear="true" Data="@powers" TValue="Power" TextProperty="Name" ValueProperty="Id" />
            <RadzenButton ButtonStyle="ButtonStyle.Secondary" Click="SaveGameAlliance" Text="Save" Style="margin-top: 20px; width: 150px;"></RadzenButton>

You can check my replies in this thread. Bind Value will attempt to set ValueProperty value to your page property. I see that ValueProperty in your case is Id which most probably is some number while selectedAlliance is an object

@Scott, you can try not setting ValueProperty. When you set it @bind-Value will be set to whatever the ValueProperty of the selected data item is.

Thank you for helping me understand the behavior of this component. I must have missed that earlier.

1 Like

I took your advice of removing ValueProperty and I was able to bind the whole object to my variable. Thank you.

1 Like

I am facing similar issue. The Data is coming as a List of Objects which have ID(Guid) and Name.
How to set Guid to ValueProperty?

<RadzenDropDown AllowClear="true" AllowFiltering="true" 
                                        LoadData="FilterRegionsList" Placeholder="Select..."
                                        Multiple="true" MaxSelectedLabels="1" 
                                        ValueProperty="RegionID" Style="width:95%">

RegionsList contais List of regions(ID : Guid and Name : String)

the issue is easy

   <RadzenDropDownDataGrid TValue="Customer" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" AllowFiltering="true" AllowClear="true"
                                    Style="width: 50%"/>

define a private value "Customer? currentCustomer"
define : List customerList (with some content from db or some source)
TextProperty is the content you see in the combobox (Customer.Name)

Whenever you change something in the box it will pick the right item for you and put it in:

Radzen is fun!