Home > Back-end >  how do i convert html edited text in C# loop to fully formatted text on the page
how do i convert html edited text in C# loop to fully formatted text on the page

Time:10-24

I have a list of strings that i would like to display in cshtml page (Asp.net C#) ,the problem is instead of having html tags in my text i want to display the text formatted on the page when i run the website. the output is: <p><strong>some </strong><i>data</i></p> it should be: some data

this is the C# code in cshtml page:

@foreach (var item in Model)
        {
            <div>@item.content</div>
        }

CodePudding user response:

You can replace HTML codes and tags with empty text using regex. Click here to verify regex

@foreach (var item in Model)
{
    <div>@Regex.Replace(item.content, "<.*?>", String.Empty)</div>
}

CodePudding user response:

You need to use Html.Raw() so it renders as IHtmlString in HTML instead of string.

<div>@Html.Raw(item.content)</div>

Rendered HTML

<div><p><strong>some </strong><i>data</i></p></div>
  • Related