在文本框中显示非英文字符

时间:2012-12-06 15:17:29

标签: html asp.net-mvc character-encoding

我想在文本框中显示一些非英文字符。

我正在尝试

$("#txtSearch").val('<%:Html.Raw(ViewBag.my_search)%>')

它应显示'2100 - KøbenhavnØ'但显示'2100 - KøbenhavnÃ''。

我的控制器从cookie中读取此值并将其分配给ViewBag。在我有控制器

ViewBag.my_search = cookie.Value 
// here it is assigning the right danish word but when it displays inside the input  box, it just displays wrong.

任何想法如何解决这个问题?

修改

嗯,它在我的本地电脑上运行良好,但是当我将它托管到一些远程托管服务提供商时,它表现不佳。

1 个答案:

答案 0 :(得分:1)

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />不是设置页面编码的好方法,因为它被真正的http标头覆盖。因此,如果远程主机提供程序正在发送内容类型标头,则将忽略它。

你的数据是正确的utf-8,所以这很好。您只需设置内容类型的http标头,以便浏览器将其读取为utf-8而不是Windows-1252。

您可以将您的个人页面设置为发送标题:

<%@ Page RequestEncoding="utf-8" ResponseEncoding="utf-8" %>

或者您可以在全局Web.config中设置它:

<configuration>
  <system.web>
    <globalization requestEncoding="utf-8" responseEncoding="utf-8" />
  </system.web>
</configuration>
相关问题