0

我编写了以下代码来接受 html 标记作为模型中的数据

 [AllowHtml]
 [Required(ErrorMessage = "Post Content Required")]
 public string PostContent { get; set; }

它接受它,但是当我回调数据时,它会显示带有 html 标签的数据,例如:

<h2><strong>&lt;p&gt;&amp;lt;p&amp;gt;[...] store and many other 
&amp;amp;hellip;&amp;lt;/p&amp;gt;&lt;/p&gt;</strong></h2>

我试图写@HttpUtility.HtmlDecode(item.PageContent),但它不会删除标签..

4

2 回答 2

0

使用 @Html.Raw 输出原始 HTML 代码。但是,这可能会使您的网站受到脚本攻击,因此请谨慎使用。

于 2013-07-23T11:57:46.237 回答
0

由于您已经在该字段上允许 html,因此我只需将 ascii 转换为 html,然后再将其发送到视图。就像是

public string Decode(string value)
    {
        return (value)
            .Replace("&quot;", "\"")
            .Replace("&lt;", "<")
            .Replace("&gt;", ">");
    }

    public string Encode(string value)
    {
        return (value)
          .Replace("\"", "&quot;")
          .Replace("'", "''")
          .Replace("<", "&lt;")
          .Replace(">", "&gt;");
    }

然后您可以在保存到数据库之前再次对其进行编码。希望这会有所帮助

于 2013-12-05T16:57:02.587 回答