javascript - 在 JavaScript 中显示来自 resources.resx 的文本

标签 javascript asp.net-mvc-3 encoding

这是 ASP.NET MVC 中的示例代码3 Razor :

@section header
{
    <script type="text/javascript">    
        $(function() {
            alert('@Resources.ExampleCompany');
        });
    </script>
}

<div>
    <h1>@Resources.ExampleCompany</h1>
</div>

上面的代码只是一个例子,但它也显示了我的编码问题。此变量@Resources.ExampleCompany 是一个文件resources.resx,其值为ExampleCompany = "Twoja firma / Twój biznes"

在 JavaScript 中,警报显示“Twoja firma / Tw&#243;j biznes”。

为什么字符 'ó' 是 'ó'?我究竟做错了什么?

在 HTML 标签中,<h1>@Resources.ExampleCompany</h1>正确显示。

更新:

Mark Schultheiss 写了一个很好的提示,我的“丑陋的解决方案”是:

var companySample = "@Resources.ExampleCompany";
$('#temp').append(companySample);
alert($('#temp').text());

现在字符是&#243;看起来不错,但这仍然不是我的问题的答案。

最佳答案

根据 HTML Encoding Strings - ASP.NET Web Forms VS Razor View Engine@ 语法自动 HTML 编码,解决方案是使用 Raw 扩展方法(例如,@Html.Raw(Resources.ExampleCompany)) 解码 HTML。尝试一下,让我们知道是否有效。

关于javascript - 在 JavaScript 中显示来自 resources.resx 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9190496/

相关文章:

来自API的奇怪字符串的java char编码

javascript - 循环动画会使浏览器崩溃吗?

javascript - 动态编写 Javascript 代码?

asp.net-mvc-3 - 当表单尚未提交时如何检索下拉选择的值

algorithm - LZW 压缩似乎无法正常工作

maven - 激活资源过滤后编码错误

javascript - 如何高效地制作可编辑行?

javascript - 如何为 JS 格式化我的 SPARQL?

javascript - Amcharts 图像 "304 Not Modified"错误

asp.net-mvc-3 - 使用 ASP.NET MVC 3 中的 ViewBag 从 Controller 传递图像并显示在 View 中