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:
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?
I'm trying now to convert the above setup to a many-to-many relationship where I have a mapping table now. How do I get this dropdown setup to work where I can select another "SyllabusItem" and have it map to the pre-requisite to a new instance of the mapping class and the context "SyllabusItem" map to the syllabusItem in the context?
Everything else in the above setup is pretty easy to convert to display the list of mapped pre-requisites, but I can't figure out how to get this multi-select dropdown setup to work w/ the map class.
Here's the mapping class:
public class PreRequisiteMap
{
public long SyllabusItemId { get; set; }
public SyllabusItem SyllabusItem { get; set; }
public long PreRequisiteId { get; set; }
public SyllabusItem PreRequisite { get; set; }
}
I have a few situations in the app where I will be having many-to-many relationships like this, and I'm just not seeing how to do this in Blazor. I see some similar examples in your Angular setups, but not sure how to fully translate this into doing it in Blazor.
There are several good examples of many-to-many relationships in Northwind database - EmployeeTerritories, Order Details, etc. You can check what code will be generated by Radzen IDE / Radzen Blazor Studio for reference:
Call me crazy, but I found a github repo for the demos you have on the website, but not for the northwind stuff... what am I missing? Can you provide a link to the repo?
After digging through those examples I don't see any that really match the use case I'm looking at. Those all have their separate CRUD pages for each of the many-to-many relationships. I'm trying to do something a bit different than that and I can't quite figure it out.
Hopefully, this description is a bit more clear.
My top level table is a "Syllabus", when I expand that row, it shows a list of "SyllabusItems". What I'm trying to do is get it so that when I edit a "SyllabusItem" row (in-line edit) that the DropDown for PreRequisites shows a list of other "SyllabusItems" on the same "Syllabus". However, I need to map the selected items in the dropdown to a new "PreRequisiteMap" where the "Syllabus" in the Context is mapped to one field and the "SelectedItem" in the DropDown value(s) selected are set to a different field in the "PreRequisiteMap" object.
Currently, I have it where it loads the data in the drop down and if I handle the Change call, I can set the PreRequisiteMap(s) properly. Here's the pertinent parts my code:
However, after I save these items and reopen for edit the "saved" PreRequisites don't show selected.
I apologize for all the questions. I'm extremely new to Blazor and this setup, and I'm likely doing something fundamentally wrong or missing something completely. I'm guessing it's how I'm binding stuff, but not fully sure.