Sandip
Sandip

Reputation: 262

Server side code encode characters automatically when used in Javascript

I have an MVC Application wherein on page load I am initializing javascript function call with a Model value received from the server.

Something like this :

myFunction('@Model.ServerUnicodeCharacters');

Now, when I set to value

Model.ServerUnicodeCharacters = 'Côte d'Ivoire'

Inside javascript I get "C &#244 ;te d&#39 ;Ivoire"

(Intentionally added space before semicolon to show the output)

Is there any way to skip this character encoding which is default in ASP.NET MVC?

Upvotes: 0

Views: 59

Answers (1)

James S
James S

Reputation: 171

try replacing:

myFunction('@Model.ServerUnicodeCharacters');

with:

myFunction('@Html.Raw(Model.ServerUnicodeCharacters)');

Taken from MSDN:

The Razor syntax @ operator HTML-encodes text before rendering it to the HTTP response. This causes the text to be displayed as regular text in the web page instead of being interpreted as HTML markup.

MSDN Reference

Upvotes: 1

Related Questions