Checkboxlist not working #481

Closed
opened 2026-01-29 17:38:01 +00:00 by claunia · 1 comment
Owner

Originally created by @MG1376 on GitHub (Jul 22, 2022).

I have this checkboxlist:

<RadzenCheckBoxList Name="chklst" Data="@listItems" TextProperty="Name" ValueProperty="Image" TValue="string" 
                    @bind-Value="chklstValues" Change="@((IEnumerable<string> a) => StateHasChanged())">
    
</RadzenCheckBoxList>

@foreach (var val in chklstValues?.ToList() ?? new List<string>())
{
    <h3>@val</h3>
}

@code {
IEnumerable<string> chklstValues = null;
class ListItem
    {
        public string Name;
        public string Image;
        public string Desc;
    }
List<ListItem> listItems = new List<ListItem>();

protected override async Task OnInitializedAsync()
 {
        await base.OnInitializedAsync();

        listItems.AddRange(new[] {
            new ListItem() { Name = "First Weather", Image = "m1", Desc = "First Weather Description hello..." },
            new ListItem() { Name = "Second Weather", Image = "m2", Desc = "Second Weather Description how ..." },
            new ListItem() { Name = "Third Weather", Image = "m3", Desc = "Third Weather Description are ..." },
            new ListItem() { Name = "Fourth Weather", Image = "m4", Desc = "Fourth Weather Description you ..." },
            new ListItem() { Name = "Fifth Weather", Image = "m5", Desc = "Fifth Weather Description hello..." },
            new ListItem() { Name = "Sixth Weather", Image = "m6", Desc = "Sixth Weather Description how ..." },
            new ListItem() { Name = "Seventh Weather", Image = "m7", Desc = "Seventh Weather Description are..." } });

}
}

But I get error:

blazor.webview.js:1 
        
       Unable to cast object of type 'ListItem' to type 'System.String'.
   at Radzen.Blazor.RadzenCheckBoxList`1.<get_allItems>b__11_0(Object i)
   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at Radzen.Blazor.RadzenCheckBoxList`1.BuildRenderTree(RenderTreeBuilder __builder)
   at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)

If I should set TValue to ListItem, What is the point for TextProperty and ValueProperty? Radzen 3.18.13
How to get it to work?

Originally created by @MG1376 on GitHub (Jul 22, 2022). I have this checkboxlist: ``` <RadzenCheckBoxList Name="chklst" Data="@listItems" TextProperty="Name" ValueProperty="Image" TValue="string" @bind-Value="chklstValues" Change="@((IEnumerable<string> a) => StateHasChanged())"> </RadzenCheckBoxList> @foreach (var val in chklstValues?.ToList() ?? new List<string>()) { <h3>@val</h3> } @code { IEnumerable<string> chklstValues = null; class ListItem { public string Name; public string Image; public string Desc; } List<ListItem> listItems = new List<ListItem>(); protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); listItems.AddRange(new[] { new ListItem() { Name = "First Weather", Image = "m1", Desc = "First Weather Description hello..." }, new ListItem() { Name = "Second Weather", Image = "m2", Desc = "Second Weather Description how ..." }, new ListItem() { Name = "Third Weather", Image = "m3", Desc = "Third Weather Description are ..." }, new ListItem() { Name = "Fourth Weather", Image = "m4", Desc = "Fourth Weather Description you ..." }, new ListItem() { Name = "Fifth Weather", Image = "m5", Desc = "Fifth Weather Description hello..." }, new ListItem() { Name = "Sixth Weather", Image = "m6", Desc = "Sixth Weather Description how ..." }, new ListItem() { Name = "Seventh Weather", Image = "m7", Desc = "Seventh Weather Description are..." } }); } } ``` But I get error: ``` blazor.webview.js:1 Unable to cast object of type 'ListItem' to type 'System.String'. at Radzen.Blazor.RadzenCheckBoxList`1.<get_allItems>b__11_0(Object i) at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext() at System.Linq.Enumerable.ConcatIterator`1.MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() at Radzen.Blazor.RadzenCheckBoxList`1.BuildRenderTree(RenderTreeBuilder __builder) at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder) at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException) ``` If I should set TValue to ListItem, What is the point for TextProperty and ValueProperty? Radzen 3.18.13 How to get it to work?
Author
Owner

@MG1376 commented on GitHub (Jul 22, 2022):

I defined Public fields instead of Properties in the class. Changed to property and everything is fine.

@MG1376 commented on GitHub (Jul 22, 2022): I defined Public fields instead of Properties in the class. Changed to property and everything is fine.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#481