Failed to set a RenderFragment property of a child component
Hi,
there is an error in browser's console when I try to pass some text to a child component via a RenderFragment property as it is described in a doc. The page is not rendered as a result.
I'm going throw the Blazor's tutorial from https://blazor.net/docs/components/index.html And I have a fault trying to execute examples from the Child content section.
I created the both ParentComponent.cshtml and ChildComponent.cshtml with the same content as described in the doc.
ParentComponent.cshtml:
@page "/ParentComponent"
<h1>Parent-child example</h1>
<ChildComponent Title="Panel Title from Parent">
Panel content of the child component is supplied by the parent component.
</ChildComponent>
ChildComponent.cshtml:
<div class="panel panel-success">
<div class="panel-heading">@Title</div>
<div class="panel-body">@PanelContent</div>
</div>
@functions {
public string Title { get; set; }
public RenderFragment PanelContent { get; set; }
}
As a result, there are a lot of errors in a browser's console after trying to render the /ParentComponent page:
Uncaught (in promise) Error: System.InvalidOperationException: Object of type 'WebApplication1.Pages.ChildComponent' does not have a property matching the name 'ChildContent'. at Microsoft.AspNetCore.Blazor.Components.ParameterCollectionExtensions.GetPropertyInfo (System.Type targetType, System.String propertyName) <0x1a16c48 + 0x00058> in <8a8022566b074cca8a70e10050945cdd>:0
The full trace:
Uncaught (in promise) Error: System.InvalidOperationException: Object of type 'WebApplication1.Pages.ChildComponent' does not have a property matching the name 'ChildContent'. at Microsoft.AspNetCore.Blazor.Components.ParameterCollectionExtensions.GetPropertyInfo (System.Type targetType, System.String propertyName) <0x1a16c48 + 0x00058> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Components.ParameterCollectionExtensions.AssignToProperty (System.Object target, Microsoft.AspNetCore.Blazor.Components.Parameter parameter) <0x1a16a70 + 0x00022> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Components.ParameterCollectionExtensions.AssignToProperties (Microsoft.AspNetCore.Blazor.Components.ParameterCollection parameterCollection, System.Object target) <0x19cc3c8 + 0x00060> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Components.BlazorComponent.SetParameters (Microsoft.AspNetCore.Blazor.Components.ParameterCollection parameters) <0x19cc0a8 + 0x00018> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder.InitializeNewComponentFrame (Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder+DiffContext& diffContext, System.Int32 frameIndex) <0x19dd138 + 0x0007a> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree (Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder+DiffContext& diffContext, System.Int32 frameIndex) <0x19dcfe0 + 0x00074> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder.InsertNewFrame (Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder+DiffContext& diffContext, System.Int32 newFrameIndex) <0x19dca70 + 0x000b4> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange (Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder+DiffContext& diffContext, System.Int32 oldStartIndex, System.Int32 oldEndIndexExcl, System.Int32 newStartIndex, System.Int32 newEndIndexExcl) <0x19dc400 + 0x0025e> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder.ComputeDiff (Microsoft.AspNetCore.Blazor.Rendering.Renderer renderer, Microsoft.AspNetCore.Blazor.Rendering.RenderBatchBuilder batchBuilder, System.Int32 componentId, Microsoft.AspNetCore.Blazor.RenderTree.ArrayRange
1[T] oldTree, Microsoft.AspNetCore.Blazor.RenderTree.ArrayRange1[T] newTree) <0x19d5b80 + 0x000a2> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Rendering.ComponentState.RenderIntoBatch (Microsoft.AspNetCore.Blazor.Rendering.RenderBatchBuilder batchBuilder, Microsoft.AspNetCore.Blazor.RenderFragment renderFragment) <0x19d2dc0 + 0x000b4> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Rendering.Renderer.RenderInExistingBatch (Microsoft.AspNetCore.Blazor.Rendering.RenderQueueEntry renderQueueEntry) <0x19d2968 + 0x00040> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Rendering.Renderer.ProcessRenderQueue () <0x19cdb80 + 0x00048> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Rendering.Renderer.AddToRenderQueue (System.Int32 componentId, Microsoft.AspNetCore.Blazor.RenderFragment renderFragment) <0x19ccbf8 + 0x00068> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Components.RenderHandle.Render (Microsoft.AspNetCore.Blazor.RenderFragment renderFragment) <0x19cc8e0 + 0x00036> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Components.BlazorComponent.StateHasChanged () <0x19cc7e8 + 0x00048> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Components.BlazorComponent.SetParameters (Microsoft.AspNetCore.Blazor.Components.ParameterCollection parameters) <0x19cc0a8 + 0x000b2> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Browser.Rendering.BrowserRenderer.AddComponent (System.Type componentType, System.String domElementSelector) <0x1995690 + 0x0005e> in <08ceae4b872f4f8d8ccefc410a9a8971>:0 at Microsoft.AspNetCore.Blazor.Browser.Rendering.BrowserRenderer.AddComponent[TComponent] (System.String domElementSelector) <0x1995380 + 0x00020> in <08ceae4b872f4f8d8ccefc410a9a8971>:0 at WebApplication1.Program.Main (System.String[] args) <0x184fbc0 + 0x00036> in <98a6978f1ec940a2bec6710e05bf3770>:0 at Microsoft.AspNetCore.Blazor.Components.ParameterCollectionExtensions.GetPropertyInfo (System.Type targetType, System.String propertyName) <0x1a16c48 + 0x00058> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Components.ParameterCollectionExtensions.AssignToProperty (System.Object target, Microsoft.AspNetCore.Blazor.Components.Parameter parameter) <0x1a16a70 + 0x00022> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Components.ParameterCollectionExtensions.AssignToProperties (Microsoft.AspNetCore.Blazor.Components.ParameterCollection parameterCollection, System.Object target) <0x19cc3c8 + 0x00060> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Components.BlazorComponent.SetParameters (Microsoft.AspNetCore.Blazor.Components.ParameterCollection parameters) <0x19cc0a8 + 0x00018> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder.InitializeNewComponentFrame (Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder+DiffContext& diffContext, System.Int32 frameIndex) <0x19dd138 + 0x0007a> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree (Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder+DiffContext& diffContext, System.Int32 frameIndex) <0x19dcfe0 + 0x00074> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder.InsertNewFrame (Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder+DiffContext& diffContext, System.Int32 newFrameIndex) <0x19dca70 + 0x000b4> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange (Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder+DiffContext& diffContext, System.Int32 oldStartIndex, System.Int32 oldEndIndexExcl, System.Int32 newStartIndex, System.Int32 newEndIndexExcl) <0x19dc400 + 0x0025e> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeDiffBuilder.ComputeDiff (Microsoft.AspNetCore.Blazor.Rendering.Renderer renderer, Microsoft.AspNetCore.Blazor.Rendering.RenderBatchBuilder batchBuilder, System.Int32 componentId, Microsoft.AspNetCore.Blazor.RenderTree.ArrayRange1[T] oldTree, Microsoft.AspNetCore.Blazor.RenderTree.ArrayRange1[T] newTree) <0x19d5b80 + 0x000a2> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Rendering.ComponentState.RenderIntoBatch (Microsoft.AspNetCore.Blazor.Rendering.RenderBatchBuilder batchBuilder, Microsoft.AspNetCore.Blazor.RenderFragment renderFragment) <0x19d2dc0 + 0x000b4> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Rendering.Renderer.RenderInExistingBatch (Microsoft.AspNetCore.Blazor.Rendering.RenderQueueEntry renderQueueEntry) <0x19d2968 + 0x00040> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Rendering.Renderer.ProcessRenderQueue () <0x19cdb80 + 0x00048> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Rendering.Renderer.AddToRenderQueue (System.Int32 componentId, Microsoft.AspNetCore.Blazor.RenderFragment renderFragment) <0x19ccbf8 + 0x00068> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Components.RenderHandle.Render (Microsoft.AspNetCore.Blazor.RenderFragment renderFragment) <0x19cc8e0 + 0x00036> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Components.BlazorComponent.StateHasChanged () <0x19cc7e8 + 0x00048> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Components.BlazorComponent.SetParameters (Microsoft.AspNetCore.Blazor.Components.ParameterCollection parameters) <0x19cc0a8 + 0x000b2> in <8a8022566b074cca8a70e10050945cdd>:0 at Microsoft.AspNetCore.Blazor.Browser.Rendering.BrowserRenderer.AddComponent (System.Type componentType, System.String domElementSelector) <0x1995690 + 0x0005e> in <08ceae4b872f4f8d8ccefc410a9a8971>:0 at Microsoft.AspNetCore.Blazor.Browser.Rendering.BrowserRenderer.AddComponent[TComponent] (System.String domElementSelector) <0x1995380 + 0x00020> in <08ceae4b872f4f8d8ccefc410a9a8971>:0 at WebApplication1.Program.Main (System.String[] args) <0x184fbc0 + 0x00036> in <98a6978f1ec940a2bec6710e05bf3770>:0 at Object.callMethod (MonoPlatform.ts:70) at Object.callEntryPoint (MonoPlatform.ts:47) at Boot.ts:38 at step (UriHelper.ts:89) at Object.next (UriHelper.ts:89) at fulfilled (UriHelper.ts:89)
I'm not sure if this is a bug or documentation is outdated. Though, I didn't find any spec if is it allowed to pass RenderFragment to a child component.
My setup:
- Windows 8
- Visual Studio 2017 15.7 preview
- .NET Core 2.1 Preview 2 SDK.
- Blazor 0.2.0
- Chrome browser 65.0.3325.181
Please rename PanelContent to ChildContent.
The documentation is incorrect. Would you be willing to file a bug about the docs at https://github.com/aspnet/blazor.docs/issues? Thanks!
Thanks for the tip!
I renamed the filed
<div class="panel panel-success">
<div class="panel-heading">@Title</div>
<div class="panel-body">@ChildContent</div>
</div>
@functions {
public string Title { get; set; }
public RenderFragment ChildContent { get; set; }
}
But there are still some errors in the console:
Uncaught (in promise) Error: System.InvalidOperationException: Unable to set property 'ChildContent' on object of type 'WebApplication1.Pages.ChildComponent'. The error was: Object of type 'Microsoft.AspNetCore.Blazor.RenderFragment' cannot be converted to type 'WebApplication1.Pages.RenderFragment'. ---> System.ArgumentException: Object of type 'Microsoft.AspNetCore.Blazor.RenderFragment' cannot be converted to type 'WebApplication1.Pages.RenderFragment'.
Do a clean build and see. it works for me
WebApplication1.Pages.RenderFragment
This suggests you have created a class with that name by mistake. Remove that class and the problem should be solved.
Yes, there was a mistake on my side when I created a page with that name: RenderFragment. @SteveSandersonMS thank you for the explanation. Now this works fine.