DataGrid binding column to list

I'm trying to get a one-to-many relationship working in a RadzenDataGrid where the user can select multiple items from a drop down box in the edit template, but I'm running into difficulties because I'm using Entity Framework Core and the underlying relationship is an ICollectible and RadzenDataGrid doesn't seem to be able to bind to this, it has to be an IEnumerable<>, at least by default. Here's the setup:

DataGrid Column:

                            <RadzenDataGridColumn TItem="SyllabusItem" Property="PreRequisites" Title="Pre-Requisites">
                                <Template Context="syllabusItem">
                                    @if (syllabusItem != null && syllabusItem.PreRequisites != null)
                                    {
                                        <RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Normal" JustifyContent="JustifyContent.Normal">
                                            @foreach (SyllabusItem item in syllabusItem.PreRequisites)
                                            {
                                                <RadzenText>@item.Name</RadzenText>
                                            }
                                        </RadzenStack>
                                    }
                                </Template>
                                <EditTemplate Context="syllabusItem">
                                    <RadzenDropDown style="display: block; width: 100%" @bind-Value="@syllabusItem.PreRequisites" Name="Pre-Requisites" TextProperty="Name"
                                                    Multiple="true" AllowClear="true" MaxSelectedLabels="2" Placeholder="Select Pre-requisite(s)"
                                                    Data="@syllabusItems?.Where(x => x.Name != syllabusItem.Name)" />
                                </EditTemplate>
                            </RadzenDataGridColumn>

Entity:

    public class SyllabusItem : BaseCTSSEntity
    {

// Other fields omitted for brevity

        [Comment("Syllabus items required before this item can be completed")]
        public virtual ICollection<SyllabusItem> PreRequisites { get; set; }


    }

With Entity Framework Core ICollection is the best way to create the relationship, but because it's an ICollection instead of an IEnumerable I get this error:

2023-03-17T15:07:52.168Z] Error: System.InvalidCastException: Unable to cast object of type 'System.Linq.EnumerableQuery`1[CTSS.Data.Model.SyllabusItem]' to type 'System.Collections.Generic.ICollection`1[CTSS.Data.Model.SyllabusItem]'.
   at Radzen.DropDownBase`1.SelectItem(Object item, Boolean raiseChange)
   at Radzen.Blazor.RadzenDropDown`1.OnSelectItem(Object item, Boolean isFromKey)
   at Radzen.Blazor.RadzenDropDown`1.OnSelectItemInternal(Object item, Boolean isFromKey)
   at Radzen.Blazor.RadzenDropDownItem`1.SelectItem(MouseEventArgs args, Boolean isclick)
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

Is there a way to get this to bind to an ICollection instead of an IEnumerable? Or do I need to do a lot of work in the code behind to have a separate IEnumerable that on row update I save to the proper ICollection?

Thanks,

Geoff

Thanks for the report @gpankretz! We will do our best to provide fix for this in our next update Monday!

1 Like