1

The goal

Fix the accentuation within <text> tag of Razor Engine.

The problem

I'm brazilian and in our country there are too much accentuation. Apple in our language means "Maçã", and as you can see, there is two accents/special characters.

The follow syntax is written in JavaScript and the application uses Razor Engine for templating.

var products = [];
@foreach (var product in Model)
{ 
    <text>
        products.push(new Product
                        ("@product.productId",
                         "@product.productName",
                         "@product.thumbnailUrl",
                         "@product.originalProductPrice",
                         "@product.promotionalProductPrice",
                         "@product.measureName",
                         "@product.measureAbbreviation",
                         "@product.marketName",
                         "@product.numberOfMarketsThatHaveThisProduct",
                         "@product.productState")
                );
    </text>
}

As you could see, there is Razor within JavaScript and JavaScript within Razor.

One result of this loop is Maçã and when I display it within <text> tag, the result is: "Ma&#231;&#227;".

I really do not know the problem nor how to solve and need your help. Any ideas?

1

1 Answer 1

1

I'm not at my Windows environment, so I apologize if this is wrong, you'll have to try it:

"@Html.Raw(product.productName)",

Razor defaults to encoding HTML entities and reserved characters.

Sign up to request clarification or add additional context in comments.

5 Comments

Oh god! Simple answer, huh? Thanks, Andrew. Worked like a charm.
I will, but you answered more faster than StackOverflow's cop can accept.
Beware of something like productName="\"'FooBar quoted'"; :)
@GuilhermeOderdenge Ah, forgot about that guy.
One (somewhat unwieldy) way to avoid the escaping problem with Html.Raw would be to use "@Ajax.JavascriptStringEncode(product.productName)", which was made for this very purpose.

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.