Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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
在JavaScript中显示resources.resx中的文本_Javascript_Asp.net Mvc 3_Encoding - Fatal编程技术网

在JavaScript中显示resources.resx中的文本

在JavaScript中显示resources.resx中的文本,javascript,asp.net-mvc-3,encoding,Javascript,Asp.net Mvc 3,Encoding,这是3中的示例代码: 现在字符是ó看起来不错,但这仍然不是我问题的答案。根据,@语法自动进行HTML编码,解决方案是使用Raw扩展方法(例如@HTML.Raw(Resources.ExampleCompany))对HTML进行解码。试试看,让我们知道这是否有效。其中一些取决于您对文本的处理方式 例如,使用标记: <div id='result'>empty</div> <div id='other'>other</div> 在浏览器中,

这是3中的示例代码:


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

根据,
@
语法自动进行HTML编码,解决方案是使用
Raw
扩展方法(例如
@HTML.Raw(Resources.ExampleCompany)
)对HTML进行解码。试试看,让我们知道这是否有效。

其中一些取决于您对文本的处理方式

例如,使用标记:

<div id='result'>empty</div>

<div id='other'>other</div>
在浏览器中,“result”标记正确显示这两个字符(如您所愿),而“other”则使用转义字符显示。两个警报都会显示转义字符

参见此处示例:。

我使用以下技巧:

<script type="text/javascript">
    $('<div/>').html("@Resources.ExampleCompany").text();
</script>

$('').html(“@Resources.ExampleCompany”).text();
也许会有帮助

更新

我更彻底地测试了Razor的这种行为,发现:

1.当文本作为html的正常内容放置时,@html.Raw方法只是帮助并写入字符“ó”,而不使用html编码(而不是作为:ó;)

例如:

@Html.Raw(“ó”)
例如:


var a=$('').html('@(“ó”)).text();//或var a='@Html.Raw(“ó”)';
console.log(a);//它显示:o
2.但如果将其作为属性放入html标记中,Razor会将其转换为:ó;@Html.Raw根本没有帮助

例如:


您可以通过将整个标记放到Resource(如那篇文章中所示)或string(如我的示例中所示)来修复它

@(“”)

因此,有时有些人可能会有点困惑,因为答案帮助了其他人,但对他没有帮助。

我也有类似的问题,但在我的例子中,我是从Resource向javascript变量赋值。字母编码也有同样的问题。之后,这个变量被绑定到一个html对象(确切地说是通过敲除绑定)。在我下面的情况下,代码给出一个技巧:

var label = '@Html.Raw(Resource.ResourceName)';

不幸的是,unescape函数没有帮助。也许这个信息可以帮助解决我的问题-只有一个字母编码为“ó”,javascript代码在一个带有html标记的文件中。如果@Resources.ExampleCompany是用标记编写的,那么我的字符“ó”将正确显示,但如果是用javascript编写的,则不正确。我不知道哪里看起来有错误,也许Razor有一个bug?更新以尝试服务器端。页面输出是否在标题中指定为UTF-8?不是,但文件是用UTF-8编码的,浏览器使用正确的编码(UTF-8)。这是默认的mvc3项目,我没有更改编码设置更新答案。请尝试此方法,并让我们知道它是否适合您。如果参考资料文本中有撇号,请参阅:
var whatitis="Twoja firma / Tw&#243;j biznes";
var whatitisnow = unescape(whatitis);
alert(whatitis);
alert(whatitisnow);
$('#result').append(whatitis+" changed to:"+whatitisnow);
$('#other').text(whatitis+" changed to:"+whatitisnow);
<script type="text/javascript">
    $('<div/>').html("@Resources.ExampleCompany").text();
</script>
<div> @Html.Raw("ó") </div>
<script type="text/javascript">
    var a = $('<div/>').html('@("ó")').text();// or var a =  '@Html.Raw("ó")';
    console.log(a); // it shows: ó
</script>
<meta name="description" content="@("ó")" />
@("<meta name="description" content="ó" />")
var label = '@Html.Raw(Resource.ResourceName)';