I have an ajax form created with the html helper: @using (Ajax.BeginForm(...))
The problem is when I post some characters like ñ, €, etc they are encoded as ñ -> ñ, €-> ⬠when they are received by the controller.
I already have this section in my Web.config:
<globalization enableClientBasedCulture="true" uiCulture="auto" culture="auto" fileEncoding="iso-8859-15" requestEncoding="iso-8859-15" responseEncoding="iso-8859-15" />
which solved previous encoding problems when passing special characters in urls.
How can I get this problem fixed so I can receive that characters in my controller?
Thanks in advance
The thing is that AJAX uses the
UTF-8encoding when it sends the request. But you have configured your server to expectrequestEncoding="iso-8859-15"which is where the discrepancy comes from.Let’s suppose that you have an input field named
Categoryin your form and the user enters theñcharacter in it. If you use a normalHtml.BeginFormthe following POST request payload will be sent to the server:On the other hand if you use an
Ajax.BeginForm, the following request POST payload will be sent to the server:In this case the server is not capable of properly decoding this UTF-8 encoded request because it expects requests to be
iso-8859-15encoded.So you could modify your web.config to use UTF-8 request and response encoding for your site. Can’t think of a valid reason in 2012 to use any other encoding than UTF-8 in a web application.
Also don’t forget to update any
<meta>tags that you might have in your layouts in order to useUTF-8as well: