ASP.NET MVC - French accent not correctly displayed in popup window

孤街醉人 提交于 2019-12-25 06:07:32

问题


I'm working with ASP.NET MVC and I'm trying to display a success (or error) message through a popup window. My message contains some french accents and there are not correctly displayed. For instance, instead of displaying "é", I got "&#233" in place. I've put these line in my part but nothing changes :

I also tried to replace my accents by the "code" but it is also not working.

Here's what I'm doing:

In my controller:

TempData["Message"] = "Rendez-vous enregistré avec succès";
return RedirectToAction("NewAppointment");

In my View (NewAppointment):

@{
    var message = TempData["Message"] ?? string.Empty;
}

<script type="text/javascript">
    var message = '@message';
    if (message)
        alert(message);
</script>

Any way to get my accents correctly displayed?


回答1:


Your string automatically gets html encoded. Prevent that by using Html.Raw():

var message = '@Html.Raw(message)';



回答2:


Use MvcHtmlString to avoid razor for htmlencoding your string :

 var message = '@(new MvcHtmlString(message))' ; 



回答3:


Use this

<script type="text/javascript">
var message = '@Html.Raw(message)';
if (message)
    alert(message);
</script>


来源:https://stackoverflow.com/questions/24570473/asp-net-mvc-french-accent-not-correctly-displayed-in-popup-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!