ListBox (Multiple Selection) InvalidCastException

Hello everyone,

I'm having some issues on multiple selection for ListBox component. The necessary code is above.

The component loads correctly with the values but I can't check any item (it doesn't tick when I click)... Also when I click on the square near the search icon (select all) i get the following exception:

System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[System.Object]' to type 'System.Collections.Generic.IEnumerable`1[System.String]'.
   at Radzen.Blazor.RadzenListBox`1.SelectAll()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

|||||||||||||||||||||||||||||||| CODE |||||||||||||||||||||||||||||||||||||||||||||||||||\

<RadzenListBox AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" TValue="IEnumerable<string>" @bind-Value="multipleLocations" Multiple="true" Data="@locations.ToList()"
                                       Change="@(args => Change(args))" Style="height:200px;" />

IEnumerable<string> multipleLocations = new string[] { };

string[] locations = new string[] { "ex1", "ex2" };

Any tips for this?

Thanks in advance!

Hi @svrebelo,

Thanks for the report! It will be fixed in the next update!

1 Like

The problem was fixed and new version of Radzen.Blazor was just published. You need to change a bit your code however since currently Data="@locations.ToList()" will cause Data property change on every cycle and the component will unable to maintain the selection.

@enchev thank you! Going to upgrade from 2.8.4 to 2.9.1

In that case, how should I bind data? Just want to know, in short, what I'll have to do.

Thanks for your help! Such a Fast fix!

Without ToList() is enough:

1 Like

@enchev Working as expected!

Thanks for the support!

I've remember one thing!

Applying the Required Validator doesn't seem to work for me. When no items are selecred, the string collection is empty but when I submit, the field says valid (border green)

UPDATE: After some debugging, if I initialize with

IEnumerable<string> multipleLocations;

instead of

IEnumerable<string> multipleLocations = new string[] { };

The validation will trigger correctly if none is selected. If you make the component dirty by selecting one item and then deselect, the validation won't trigger. What I see here is to force the variable being null on Change event.