skip to Main Content

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>
        }

2

Answers


  1. 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>
    }
    
    Login or Signup to reply.
  2. 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search