Problem with accentuation

0

I'm populating a list in javascript with a ViewBag . The 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 list, they lose the accent, leaving only strange characters.

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

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" in my tag <script> but I did not succeed.

  

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

How can I resolve this?

    
asked by anonymous 28.08.2017 / 19:03

1 answer

1

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

  

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

Using Html.Raw() when creating my list, it looks 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);
}

In this way, words with special characters are inserted correctly into javascript .

    
28.08.2017 / 19:30