Fix: How to render Blazor component with RenderFragment Parameter from Razor Page

 In Blazor, you can render a component with a `RenderFragment` parameter from a Razor page. Here's how you can do it:


Assume you have a Blazor component called `MyComponent` that takes a `RenderFragment` parameter:


```csharp

@page "/myrazorpage"


<h3>My Razor Page</h3>


<MyComponent>

    <ChildContent>

        <p>This is the content passed from the Razor page.</p>

    </ChildContent>

</MyComponent>

```


In the Razor page, you include the `MyComponent` component and pass a `RenderFragment` as the `ChildContent`. The `ChildContent` parameter is a `RenderFragment` in the `MyComponent` component, and you can use it within the component's markup like this:


```csharp

@page "/myrazorpage"


<h3>My Razor Page</h3>


<MyComponent>

    <ChildContent>

        <p>This is the content passed from the Razor page.</p>

    </ChildContent>

</MyComponent>

```


In the `MyComponent` component, you can use the `ChildContent` parameter like this:


```csharp

@inherits ComponentBase


<div class="my-component">

    <h4>MyComponent</h4>

    @ChildContent

</div>


@code {

    [Parameter]

    public RenderFragment ChildContent { get; set; }

}

```


This allows you to pass content from the Razor page into the `MyComponent` component and render it within the component's markup. The content you pass to the `ChildContent` parameter will be displayed where `@ChildContent` is used within the `MyComponent` component.

Comments

Popular posts from this blog

bad character U+002D '-' in my helm template

GitLab pipeline stopped working with invalid yaml error

How do I add a printer in OpenSUSE which is being shared by a CUPS print server?