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
at

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

  <RadzenDropDown id="CountryName" AllowClear="true" TValue="string" Placeholder="Select..."
                                                        FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                                                        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"
                                        FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                                        FilterOperator="StringFilterOperator.Contains" AllowFiltering="true"
                                        Data="@countryList" TextProperty="Country_Name" ValueProperty="CountryID_PK"
                                        @bind-Value="_Organization.CountryID_FK"
                                        Change="@SelectedCountryChangeEvent"
                                        SelectedItemChanged="@(obj => SelectedCountryChanged(obj))" />

@countrylist
has Country_Name(String) & CountryID_PK (GUID)

OrgTable
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>
            <div>
                <RadzenDropDown @bind-Value="@selectedAlliance" AllowClear="true" Data="@alliances" TValue="Alliance" TextProperty="Name" ValueProperty="Id" />
            </div>
            <div>
                <RadzenDropDown @bind-Value="@selectedPower" AllowClear="true" Data="@powers" TValue="Power" TextProperty="Name" ValueProperty="Id" />
            </div>
            <RadzenButton ButtonStyle="ButtonStyle.Secondary" Click="SaveGameAlliance" Text="Save" Style="margin-top: 20px; width: 150px;"></RadzenButton>
        </div>
    </div>
</RadzenCard>);

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