0

I have an issue that´s giving me many problems. I need to set from an cshtml using C# code some variables to javascript. The problem is that accutes and the spanish "ñ" are showed like unicode strings in the window.

I´m trying this code:

@:var nombrePersona = '@System.Text.Encoding.Unicode.GetString(System.Text.Encoding.Unicode.GetBytes(participante.Nombre))'
@:var apellidoPersona = '@System.Text.Encoding.Unicode.GetString(System.Text.Encoding.Unicode.GetBytes(participante.Apellidos))'

And the result in javascript is like:

"Jes'&'#250;s" and it should be "Jesús"

Any idea of how to fix this?

Thank you

2 Answers 2

1

Try this:

var nombrePersona = '@Html.Raw(HttpUtility.HtmlDecode(participante.Nombre))'
var apellidoPersona = '@Html.Raw(HttpUtility.HtmlDecode(participante.Apellido))'

EDIT

Also. If you fill HtmlDecode() with a badly formatted string, it won't work. Try cleaning it first:

"Jes'&'#250;s".Replace("'", "")  // "Jesús" 
Sign up to request clarification or add additional context in comments.

Comments

1

You can try this:

var nombrePersona = '@Html.Raw(HttpUtility.JavaScriptStringEncode(participante.Nombre))'
var apellidoPersona = '@Html.Raw(HttpUtility.JavaScriptStringEncode(participante.Apellido))'

Saludos.

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.