如何在视图中将MVC模型字符串作为纯文本

Esp*_*pen 8 javascript asp.net-mvc html-encode html-entities razor

我正在向具有字符串的视图发送模型.这些字符串是html编码的,我不需要它们.没有html编码的任何方式将模型发送到视图?

模型:

public class Package
{
    public string String { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

public ActionResult GetPackage()
{
    Package oPackage = new Package();
    oPackage.String = "using lots of \" and ' in this string";
    return View(oPackage);
}
Run Code Online (Sandbox Code Playgroud)

视图:

@model Models.Package
<script type="text/javascript">
    (function () {
        // Here @Model.String has lots of &#39; and &quot;
        var String = "@Model.String".replace(/&#39;/g, "'").replace(/&quot;/g, "\"");
        // Here String looks ok because I run the two replace functions. But it is possible to just get the string clean into the view?
    })();
</script>
Run Code Online (Sandbox Code Playgroud)

运行替换函数是一种解决方案,但只是在没有编码的情况下获取字符串会很棒.

Par*_*osh 11

@Html.Raw(yourString)
Run Code Online (Sandbox Code Playgroud)

这应该工作:

@model Models.Package
<script type="text/javascript">
    (function () {
      var String = "@Html.Raw(Model.String)";
})();
</script>
Run Code Online (Sandbox Code Playgroud)