InvalidCastException on selecting from ListBox

I'm implementing what should be a relatively straightforward listbox; my intent is for it to be bound to a field (schoolID) in the model I'm using (regForm) for the EditContext of the page. The data is pulled from my DB into a list (SIDSchools) of pretty standard objects based off the table, with string name field (SchoolName) and int primary key ID (SchoolID).

The declaration for the listbox is:

            <RadzenListBox AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" @bind-Value="regForm.schoolID" TValue="int" Data="@SIDSchools" TextProperty="SchoolName" ValueProperty="SchoolID"
                Style="margin-bottom: 20px; height:200px;" />

While this populates the listbox without a problem, and the filtering works fine, if I try to select an item in the list, Blazor throws out an error and the browser shows that there was an InvalidCastException:

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.DropDownBase`1[T].SelectItem (System.Object item, System.Boolean raiseChange) <0x379d868 + 0x0037c> in <filename unknown>:0 at Radzen.Blazor.RadzenListBox`1+<>c__DisplayClass0_0[TValue].<BuildRenderTree>b__6 () <0x379c848 + 0x000d4> in <filename unknown>:0 at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion (System.Threading.Tasks.Task task) <0x311ff48 + 0x000da> in <filename unknown>:0 at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x3120a90 + 0x000b6> in <filename unknown>:0

I'm not sure why this is happening as I'm specifying that the value is an int, and both the SchoolID field in the School object and the schoolID field in the model are ints. One other issue I've noticed is that despite regForm.schoolID having a value, nothing is selected after initialization (there are also no errors). The ID fields had previously been nullable ints; I was concerned that the nullable part may have been the problem and converted them to ints (changing TValue appropriately) to check.

I feel like I'm doing something wrong in binding the value, but as I'm new to the package it's not clear to me what exactly it is. Any help with this would be greatly appreciated, thanks in advance!

1 Like

We can't tell what is causing this issue without somehow reproducing it. If you have a Radzen subscription you can send your project to info@radzen.com.