如何在textareafor中解码html代码?
我已经编写了以下代码来接受html标记作为模型中的数据如何在textareafor中解码html代码?,html,asp.net-mvc,Html,Asp.net Mvc,我已经编写了以下代码来接受html标记作为模型中的数据 [AllowHtml] [Required(ErrorMessage = "Post Content Required")] public string PostContent { get; set; } 它接受它,但当我回调数据时,它会显示带有html标记的数据,如: <h2><strong><p>&lt;p&gt;[...] store and
[AllowHtml]
[Required(ErrorMessage = "Post Content Required")]
public string PostContent { get; set; }
它接受它,但当我回调数据时,它会显示带有html标记的数据,如:
<h2><strong><p>&lt;p&gt;[...] store and many other
&amp;hellip;&lt;/p&gt;</p></strong></h2>
p&;书信电报;宝洁;燃气轮机;[…]商店和许多其他商店
&;amp;hellip&;lt/宝洁;燃气轮机/p
我试图编写
@HttpUtility.HtmlDecode(item.PageContent)
,但它没有删除标记。使用@Html.Raw输出原始Html代码。但是,这可能会使您的站点受到脚本攻击,因此请小心使用。因为您已经允许在字段中使用html,所以在将其发送到视图之前,我只需将ascii转换为html。差不多
public string Decode(string value)
{
return (value)
.Replace(""", "\"")
.Replace("<", "<")
.Replace(">", ">");
}
public string Encode(string value)
{
return (value)
.Replace("\"", """)
.Replace("'", "''")
.Replace("<", "<")
.Replace(">", ">");
}
公共字符串解码(字符串值)
{
返回(值)
.Replace(“”,“\”)
.替换(“,”);
}
公共字符串编码(字符串值)
{
返回(值)
.替换(“\”,“”)
.替换(“”,“”)
.替换(“,”);
}
然后可以在保存到数据库之前再次对其进行编码。希望这会有所帮助您是在尝试剥离HTML标记,还是在尝试显示包含标记的原始文本?您试图生成的源代码是什么?嗯,这个文本以
开头,以结尾,而解码文本也以开头,以结尾,这并不意味着HtmlDecode没有做任何事情!但是我只想要没有的简单文本 那么我该怎么办呢???我试过@Html.Raw,但它显示的文本格式与我在编辑器中格式化的文本格式相同,但仍然显示标签。。