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" })
3
  • maybe Gu's blog is helpful.hanselman.com/blog/… Commented May 20, 2014 at 0:52
  • @Larry you have a minor syntax error the comment. You meant Scott Hanselman and not Scott Guthrie :) Commented May 20, 2014 at 0:58
  • See my answer here: stackoverflow.com/questions/7889038/… Commented May 20, 2014 at 2:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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}.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.