Home > database >  How can I render html in a field in Blazor
How can I render html in a field in Blazor

Time:10-13

I have a field saved in the DB with HTML. I am using TinyMCE for my text editor and it is correctly saving the HTML tags in the DB. However, when I render the field, it still shows the tags. Initaily I had this:

<td>
    @objInv.Notes
</td>

My latest attempt to resolve this is:

<td>
    @(new HtmlString(objInv.Notes))
</td>

Either way, it still renders as:

<p>New laptops 09/07/2022 <strong>test</strong></p>

What I desire is:

New laptops 09/07/2022 test

CodePudding user response:

Raw HTML can be rendered in Blazor by using the MarkupString. You can set the raw HTML as a string to any parameter and cast it in a markup string.

You can render it like this:

<table >
    <thead>
    <tr>
        <th>Notes</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>@((MarkupString)myNote)</td>
    </tr>
    </tbody>
</table>

@code {
    string myNote = "<p>New laptops 09/07/2022 <strong>test</strong></p>";
}

Output:

Output

  • Related