Reputation: 3242
I have an ASP.Net MVC View receiving a strongly typed model:
@model MyApp.Models.MyModel
When displaying properties values of this model,
for example: @Html.DisplayFor(model => model.myFrenchProperty)
french characters are not shown correctly in browser.
For example the french word "Général" is shown as "Général" in browser.
I have try using @Html.Raw(Model.myFrenchProperty) but nothing change.
My model is built with Entity Framework and data come from an UTF-8 encoded database.
Any idea how to make this work properly?
Upvotes: 5
Views: 5674
Reputation: 487
I was trying to display "Déconnecter" but it was appearing in the HTML as
"Déconnecter"
. The issue was ocurring when passing razor model properties into jquery functions that then converted the text to HTML.
The solution for me was to pass the variable into the following:
"@Html.Raw(myVar)" "@Html.Raw("Déconnecter")"
Upvotes: 1
Reputation: 461
This happens because when the web page renders, the web browser by default displays the characters as HTML encoded.
In order for any French or Latin character like accentuations to show correctly, we need to decode the desired string.
HttpUtility.HtmlDecode(myFrenchProperty)
Should do the work.
Upvotes: 0
Reputation: 3242
I'm using PostgreSQL database with Devart connector and the problem was specific to this setup... My problem was solved adding Unicode=true in the connection string... It seems that having an UTF-8 configured database is not enough...
Etienne
Upvotes: 1
Reputation: 9541
You need to make sure that the page is rendered using UTF-8 encoding as well. In order to do so you need to add:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
or
<meta charset="utf-8">
(if you are using HTML5)
in your <head>
tag.
Upvotes: 2