简体   繁体   English

我如何将 C# 循环中的 html 编辑文本转换为页面上的完全格式化文本

[英]how do i convert html edited text in C# loop to fully formatted text on the page

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.我有一个我想在 cshtml 页面(Asp.net C#)中显示的字符串列表,问题是我想在运行网站时显示页面上格式化的文本,而不是在我的文本中包含 html 标签。 the output is: <p><strong>some </strong><i>data</i></p> it should be: some data输出是: <p><strong>some </strong><i>data</i></p>它应该是:一些数据

this is the C# code in cshtml page:这是 cshtml 页面中的 C# 代码:

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

You can replace HTML codes and tags with empty text using regex.您可以使用正则表达式将 HTML 代码和标签替换为空文本。 Click here to verify regex单击此处验证正则表达式

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

You need to use Html.Raw() so it renders as IHtmlString in HTML instead of string .您需要使用Html.Raw()以便它在 HTML 中呈现为IHtmlString而不是string

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

Rendered HTML呈现的 HTML

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM