这是 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ój biznes
”。
为什么字符 'ó' 是 'ó'?我究竟做错了什么?
在 HTML 标签中,<h1>@Resources.ExampleCompany</h1>
正确显示。
更新:
Mark Schultheiss 写了一个很好的提示,我的“丑陋的解决方案”是:
var companySample = "@Resources.ExampleCompany";
$('#temp').append(companySample);
alert($('#temp').text());
现在字符是ó
看起来不错,但这仍然不是我的问题的答案。
最佳答案
根据 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/