Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在textareafor中解码html代码?_Html_Asp.net Mvc - Fatal编程技术网

如何在textareafor中解码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>&lt;p&gt;&amp;lt;p&amp;gt;[...] store and

我已经编写了以下代码来接受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>
p&;书信电报;宝洁;燃气轮机;[…]商店和许多其他商店
&;amp;hellip&;lt/宝洁;燃气轮机/p

我试图编写
@HttpUtility.HtmlDecode(item.PageContent)
,但它没有删除标记。

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

因为您已经允许在字段中使用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;");
    }
公共字符串解码(字符串值)
{
返回(值)
.Replace(“”,“\”)
.替换(“,”);
}
公共字符串编码(字符串值)
{
返回(值)
.替换(“\”,“”)
.替换(“”,“”)
.替换(“,”);
}

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

您是在尝试剥离HTML标记,还是在尝试显示包含标记的原始文本?您试图生成的源代码是什么?嗯,这个文本以
开头,以
结尾,而解码文本也以
开头,以
结尾,这并不意味着HtmlDecode没有做任何事情!但是我只想要没有的简单文本

那么我该怎么办呢???我试过@Html.Raw,但它显示的文本格式与我在编辑器中格式化的文本格式相同,但仍然显示标签。。