Skip to content

Blazor - Save result of razor component to a string #29648

@mrlife

Description

@mrlife

Is your feature request related to a problem? Please describe.

Sometimes I have to pass HTML to JavaScript to use it in a JavaScript-enhanced element (Bootstrap "plugin"). It would be useful to be able to reuse an existing razor component by saving the output it would render to a string and passing it on to JavaScript.

Describe the solution you'd like

Given a component MyComponent.razor:

<h1>@Title</h1>
<p>@Description</p>

code {
    [Parameter]
    public string Title { get; set; }

    [Parameter]
    public string Description { get; set; }
}

Save its output to a string and use like:

<div class="my-plugin" data-content="@myComponentOutput"></div>

code {
    private string myComponentOutput;
    private MyComponent myComponent;

    protected override void OnInitialized()
    {
        myComponentOutput = myComponent.GetOutput(Title: "Hello", Description: "Just saying hello");
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions