0

In my ASP.NET MVC 3 project, I have set the character encoding in my master page

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

then, in my view, I have

    <script type="text/javascript" charset='UTF-8'>
        $(function () {
            $('#my-btn').click(function () {
                  $(this).val('@MyProject.Resources.OrderButton');
            });
        });
    </script>

what gives me the value Zam&#243;w onstead of Zamów. The resource file's first line is:

<?xml version="1.0" encoding="utf-8"?>

Any ideas how to fix it ?

1 Answer 1

3

The correct way to pass server side values to javascript variables is the following:

var value = @Html.Raw(Json.Encode(MyProject.Resources.OrderButton);
$(this).val(value);

This will output code which is completely safe and correctly encoded to be passed to a javascript function. This will also properly handle cases where your string contains characters such as ', new lines, ... which would have broken your javascript code.

And you should not care whether some characters are HTML or whatever encoded. The important thing is that they will be correctly encoded for a browser or an HTML compliant client to correctly consume.

Sign up to request clarification or add additional context in comments.

1 Comment

Dunno why, but the ReSharper intellisense whine about syntax error when writing without ''.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.