Josh Blade
Josh Blade

Reputation: 981

MVC EditorFor Datetime localization

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" })

Upvotes: 0

Views: 1148

Answers (1)

Anupam Singh
Anupam Singh

Reputation: 1166

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.

Upvotes: 1

Related Questions