stitch7c0 stitch7c0 - 1 month ago 9
Javascript Question

Showing text from resources.resx in JavaScript

This is example code in ASP.NET MVC 3 Razor:

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

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


The code above this is just an example, but it also shows my problem with encoding. This variable @Resources.ExampleCompany is a file resources.resx with value
ExampleCompany = "Twoja firma / Twój biznes"


In JavaScript, the alert shows the "
Twoja firma / Tw&#243;j biznes
".

Why is character 'ó' '&#243'? What am I doing wrong?

In HTML tag,
<h1>@Resources.ExampleCompany</h1>
is displayed correctly.

UPDATE:

Mark Schultheiss wrote a good hint and my "ugly solution" is:

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


Now the character is
&#243;
and looks good, but this is still not answer to my issue.

Answer

According to HTML Encoding Strings - Web Forms VS Razor View Engines, the @ syntax automatically HTML encodes and the solution is to use the Raw extension-method (e.g., @Html.Raw(Resources.ExampleCompany)) to decode the HTML. Try that and let us know if that works.

Comments