RadzenDropDown show name but return Id

Hopefully you can help me with something I thought I had figured out!

I have a dropdown on an 'add device' form.
The data for the dropdown is a simple list (int ContactsId, string CombinedName).
I want to display the name in the dropdown (for the user to select), but when the form is submitted I want the associated ContactsId to be saved in the new device record.

I have my dropdown set up as follows:

<RadzenDropDown Data="@contactList" style="display: block; width: 100%"
                @bind-Value="@device.ContactsId" ValueProperty="ContactsId" TextProperty="CombinedName" Name="ContactsId"/>

When I make a selection in the dropdown I get the following error:

"System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Nullable`1[System.Int32]'."

So clearly it is trying to add CombinedName (string) into @device.ContactsId (int).

I thought setting the ValueProperty to ContactsId should have handled this?

Please Help! :o)

Did you try setting the tvalue property

<RadzenDropDown TValue="int" Data="@contactList" style="display: block; width: 100%"
                @bind-Value="@device.ContactsId" ValueProperty="ContactsId" TextProperty="CombinedName" Name="ContactsId"/>

Yep, had already tried that but it throws more errors:

CS1662: Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

CS1503: Argument 2: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback<int?>' to 'Microsoft.AspNetCore.Components.EventCallback'.

CS0266: Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)

<RadzenDropDown TValue="int?" Data="@contactList" style="display: block; width: 100%"
                @bind-Value="@device.ContactsId" ValueProperty="ContactsId" TextProperty="CombinedName" Name="ContactsId"/>

This should resolve the issue

1 Like

Yep, that fixed it. Amazing! Thank you :o)