2

I have a form that is sits in a plain .html file with the following meta tag:

<meta http-equiv="Content-Type" content="text/html; charset=shift_jis">

The service I'm posting a form to requires that the data is in shift-jis encoding. When I post this form by just opening this .html file with Chrome, then clicking a submit button, the service accepts it fine. When I inspect the post values in Fiddler, the Japanese characters appear in the post to the service like follows:

goods_name_1 = "���i�P"

Now when I take the exact same form and place it in an ASP MVC view, serve the view to Chrome with its source identical to the one I have in the .html file, view source looks exactly the same as the .html version opened did. But the problem is when I post the form with a submit button, the post values look like this:

goods_name_1 = "商品1"

The service then replies with an encoding issue.

Can anyone suggest what might be going wrong? The view served from ASP MVC has the response header "Content-Type:text/html; charset=utf-8". Im not sure why the post values aren't the same as the .HTML file version though. Any ideas?

Just to add, the .html file I have is saved as Unicode in windows.

Thanks.

Martin Blore
  • 2,125
  • 4
  • 22
  • 34
  • Have you tried changing the encoding headers in the response (Response.Headers["Content-Type"] = "charset=shift_jis";)? – JTMon Jun 15 '15 at 09:28
  • Are you setting culture? http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx – Dave Alperovich Jun 15 '15 at 18:52

1 Answers1

1

Set the ContentEncoding of the response either in your Controller:

public ActionResult MyAction() {
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("shift_jis");
    return View();
}

Or in the View:

@{
    ViewBag.Title = "Home Page";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("shift_jis");
}
Paul Taylor
  • 5,651
  • 5
  • 44
  • 68
  • 1
    Hi Paul. I tried this but unfortunately it didn't work. Just as I was typing out the property however, I spotted "ContentEncoding". I've been after a way to try and control the returned encoding that ASP MVC was using to test if that might work so I thought great, I'll give this a shot. I added "Response.ContentEncoding = System.Text.Encoding.GetEncoding("shift_jis");" in the same place, tested the post, and bingo! It works! So thanks for pointing me in a new direction. Can you update your answer and I'll give you the bounty :). Thanks again, 3 days I've been stuck on this sucker! – Martin Blore Jun 17 '15 at 14:04