LoadOnDemand with child nodes represented by interface instead class generate exception
Hy,
my blazor server app has a compnent with a SfTreeView like this:
<div class="content-area">
<SfTreeView @ref="TreeViewReference" TValue="ICommessaElementTreeViewItem" LoadOnDemand="true">
<TreeViewEvents TValue="ICommessaElementTreeViewItem" NodeExpanding="OnExpanding" NodeSelected="OnSelect" NodeClicked="OnNodeClicked"></TreeViewEvents>
<TreeViewFieldsSettings TValue="ICommessaElementTreeViewItem" DataSource="@ViewModel.ListaVM" Id="RowGuid" Text="Descrizione" HasChildren="HasChildren" Child="Children" Selected="IsSelected"></TreeViewFieldsSettings>
</SfTreeView>
</div>
where ICommessaElementTreeViewItem is an interface implemented by this class:
publicclass CommessaElementTreeViewItem : ICommessaElementTreeViewItem
{
....
public ObservableCollection<CommessaElementTreeViewItem> Children { get; set; }
IEnumerable<ICommessaElementTreeViewItem> ICommessaElementTreeViewItem.Children =>
Children.ToObservableCollection<ICommessaElementTreeViewItem>();
.....
}
OnExpanding Method of SfTreeGird fill Children Property with children CommessaElementTreeViewItem in a complex business process but this is not the problem.
Mi App works very well before upgrade syncfusion component to 30.1.38. and now when I click on a root element to epand it throw this exception:
System.InvalidCastException: Unable to cast object of type 'System.Collections.ObjectModel.ObservableCollection`1[Tonin.Commessa.Gestione.ViewModel.Element.CommessaElementTreeViewItem]' to type 'System.Collections.Generic.List`1[Tonin.Commessa.Gestione.ViewModel.Infrastructure.Element.ICommessaElementTreeViewItem]'.
in other word SfTreeView thinks that child nodes are ICommessaElementTreeViewItem instead they are CommessaElementTreeViewItem (interface instead class).
this works very well before upgrade and the problemis only when click on expand a node (Excepiton not throwed on root elments.
this is the exception call stack:
System.InvalidCastException: Exception
---> System.InvalidCastException: Unable to cast object of type 'System.Collections.ObjectModel.ObservableCollection`1[Tonin.Commessa.Gestione.ViewModel.Element.CommessaElementTreeViewItem]' to type 'System.Collections.Generic.List`1[Tonin.Commessa.Gestione.ViewModel.Infrastructure.Element.ICommessaElementTreeViewItem]'.
at Syncfusion.Blazor.Navigations.Internal.ListGeneration`1.GetOptions(TreeViewFieldsSettings`1 fields, Boolean isSelected, String isChecked, Boolean isEdit, Boolean isDisable, Boolean isLoaded)
at Syncfusion.Blazor.Navigations.Internal.ListGeneration`1.RenderingHierarchicalData(TreeItemCreatedArgs`1 args)
at Syncfusion.Blazor.Navigations.Internal.ListGeneration`1.BeforeNodeCreate(TreeItemCreatedArgs`1 args)
at Syncfusion.Blazor.Navigations.Internal.CreateListFromComplex`1.InvokeTreeItemCreating(TValue item, Int32 nodeLevel)
--- End of inner exception stack trace ---
at Syncfusion.Blazor.Navigations.Internal.CreateListFromComplex`1.InvokeTreeItemCreating(TValue item, Int32 nodeLevel)
at Syncfusion.Blazor.Navigations.Internal.CreateListFromComplex`1.BuildRenderTree(RenderTreeBuilder __builder)
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
log @ blazor.server.js:1
blazor.server.js:1 [2025-07-04T06:11:50.732Z] Information: Connection disconnected.
[NEW] Explain Console errors by using Copilot in Edge: click
to explain an error.
Learn more
Don't show again
What can I do?
Rolando
Hi Rolando Tonin,
Based on the details you shared, we prepared a sample that closely follows the structure you described. However, we could not reproduce the issue on our end.
Since the information provided was partial, we couldn’t fully recreate your exact scenario. Also, we’re not sure about the specific configuration in which you're facing the issue.
To help us proceed further and validate the problem accurately, it would be helpful if you could share some additional details.
If you're able to reproduce the issue in the sample we’ve shared, or if you can modify it to reflect your scenario and share it back with us, that would also help us validate the issue and provide a solution more efficiently.
Sample: Attached as a zip file.
Looking forward to your response.
Regards,
Saravanan J
Attachment: BlazorApp1_a41e50ab.zip
My solution is very complex, consisting of 140 projects based on common business logic and MVVM shared code between Blazor, WPF, and Maui App and library.
I couldn't reconstruct the error starting from the basic example you provided. However, I used some parts of your example, such as FindByID method, following cast:
public List<ICommessaElementTreeViewItem> Children =>
ConcreteChildren.Cast<ICommessaElementTreeViewItem>().ToList();
and, furthermore, the concrete class derived from a generic abstract treeviewitem base class, which I completely rewrote, simplifying it.
Now it seems to work. In this simplified code review, I probably removed some connection between the abstract and concrete classes that was causing this problem with casting error message between concrete objects and the interface.
What I don't understand is that before version 30.1.38, SfTreeView worked for over a year without any problems.
In any case, the important thing is that it now also works with the recent release of the component.
Thank you very much
Rolando
Hi Rolando,
Thank you for your detailed update and for sharing how you resolved the issue. We are glad to hear that your solution is now working with the recent release of the component. If you have any further questions or require assistance in the future, please do not hesitate to reach out.
- 3 Replies
- 3 Participants
-
RT Rolando Tonin
- Jul 3, 2025 05:03 PM UTC
- Jul 16, 2025 05:36 PM UTC