1

I am using a jQuery plug-in to display data. The plug-in basically works on JavaScript arrays. I have written the code as below:

var users = [];
@foreach(MyApp.UserModel um in Model.Users)
{
    @:users.push('@um.NameSurname');
}

to push data into a JavaScript array. Actually this code works fine. But I have character issues. Some of the names contain Turkish characters such as "Ş", "Ğ", "Ü", "Ö" which are displayed incorrectly. For example Ç is converted to Ç. In my code I have defined localization as :

<head>
    <meta charset="utf-8" lang="tr" />
</head>

and my JavaScript code looks like

users.push('Test &#199;ADIR');

instead of

users.push('Test ÇADIR');

What am I doing wrong and how can I fix this?

1
  • You can just use var users = @Html.Raw(Json.Encode(Model.Users)) to create an array from your collection property. Commented Feb 7, 2018 at 21:17

1 Answer 1

2

Convert your string to an IHtmlString to avoid razor encoding the value.

IHtmlString represents an HTML-encoded string that should not be encoded again.

var users = [];
@foreach(MyApp.UserModel um in Model.Users)
{
    var name = new HtmlString(um.NameSurname);
    @:users.push('@name');
}

Or if you are if your prefer Html.Raw():

var users = [];
@foreach(MyApp.UserModel um in Model.Users)
{
    @:users.push('@Html.Raw(um.NameSurname)');
}
Sign up to request clarification or add additional context in comments.

Comments

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.