0

I have a localized website that allows the user to choose the language.

My date fields always display in US format (mm/dd/yyyy), but my model binder is expecting the properly localized string. So when the user is working in a EURO language where the format should be dd/mm/yyyy, the page loads with the incorrect date format (and then causes validation errors if not fixed).

I'm unsure of how to dynamically set the format of the date string when loading the page through the html helpers for editorfor or textboxfor.

Update: Here's the code for the editorfor template that I made after reading Anupam's suggestion

@model DateTime?
@Html.TextBox("", String.Format("{0:d}", Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "date" })
Josh Blade
  • 981
  • 2
  • 10
  • 24
  • maybe Gu's blog is helpful.http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx – Larry May 20 '14 at 00:52
  • @Larry you have a minor syntax error the comment. You meant Scott Hanselman and not Scott Guthrie :) – Yogiraj May 20 '14 at 00:58
  • See my answer here: http://stackoverflow.com/questions/7889038/asp-net-mvc-datetime-culture-issue-when-passing-value-back-to-controller/19599068#19599068 – Jeff Dunlop May 20 '14 at 02:40

1 Answers1

1

Try to create custom template for Date ..

read my article here

http://www.codeproject.com/Articles/672591/Exploring-Display-and-Editor-Templates-in-ASP-NET

hope it will help.

Anupam Singh
  • 1,158
  • 13
  • 25
  • Thanks. A simple DateTime EditorFor Template did the trick. I just had to convert my date to a string and format it with {0:d}. – Josh Blade May 20 '14 at 22:08