Problem with accentuation

Asked

Viewed 586 times

0

I’m populating a list on javascript through a ViewBag. To ViewBag has a list of objects and these objects have properties with accented words. They are coming correctly from ViewBag, but when I add the object to the javascript, they lose their accentuation, leaving only strange characters.

In a word like Verification, it stays Verificação.

I populate my list as follows:

@foreach(var item in ViewBag.ListRequired)
{
    @:var required = {
    @:ExamNumber: "@item.ExamNumber",
    @:TypeName: "@item.TypeName",
    @:Description: "@item.Description"
    @:};

    @:listRequiredContent.push(required);
}

I tried to put charset="utf-8" on my tag <script> but I was unsuccessful.

There are fields on the page that have accentuation and the text is shown correctly. The problem is only when I try to manage the information via javascript.

How can I fix this?

1 answer

1


I solved the problem using Html.Raw() when sending texts to javascript. It’s a safe way to insert texts into javascript.

The Html.Raw() returns strings without html encoding.

Using Html.Raw() when I created my list, it became like this:

@foreach(var item in ViewBag.ListRequired)
{
    @:var required = {
    @:ExamNumber: "@Html.Raw(item.ExamNumber)",
    @:TypeName: "@Html.Raw(item.TypeName)",
    @:Description: "@Html.Raw(item.Description)"
    @:};

    @:listRequiredContent.push(required);
}

Thus, words with special characters are inserted correctly in the javascript.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.