C#将HTML显示为纯文本

时间:2014-10-24 15:39:59

标签: c# asp.net-mvc-4

在cshtml文件中,我使用的是Controller中定义的字符串(由模型处理):

控制器:

namespace Site.Controllers
{
    public class PController : Controller    
    {
        public static P P1 = new P("<p>This is a paragraph</p>");
        public ActionResult Index()
        {
            return View(P1);
        }
    }
}

型号:

public class P(string paragraph)
{
    public String Paragraph { get; set; }
    public P(string paragraph ="")
    {
        Paragraph = paragraph;
    }
}

CSHTML:

@Site.Controllers.PController.P1.Paragraph 

CSHTML中的段落结果为<p>This is a paragraph</p>而不是

这是段落

如何让HTML像HTML一样处理?

1 个答案:

答案 0 :(得分:2)

尝试

@Html.Raw(Site.Controllers.PController.P1.Paragraph)

此方法创建HtmlString的实例。

它表示应按原样使用的HTML编码字符串。

然后它包装此实例并将其显示为HTML标记。