0

I'm wokring an a multi-culture site which needs to validate form input, specifically the date entered by a user on the front end.

The problem i'm having is that vallues for annotations need to be constants,. I currently have:

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public string PreferredDate { get; set; }

However I would like the date format to be determined at runtime based on the culture, for example en-US would be MM/dd/yyyy whilst en-GB would be dd/MM/yyyy as above.

The problem as stated is that attribute arguments must be constants, so I can't do somethign like:

[DisplayFormat(DataFormatString = "{0:" + CultureInfo.CurrentCulture.DateTimeFormat.ToString() + "}", ApplyFormatInEditMode = true)]

Is there anyway around this?

Dave0504
  • 1,057
  • 11
  • 30
  • Well, is not a simple subject. When you have two culture to manage you need to localize your application. That will involve multiple layers of your application (from javascript to controllers/action and html/cshtml) – Ionut Ungureanu Jun 07 '17 at 08:11
  • Since you can't have 2 `DisplayFormatAttribute` on a property and attribute arguments require constant values, you need to consider custom `IModelBinder` by changing default model binder depending on user's culture, see https://stackoverflow.com/questions/7889038/asp-net-mvc-datetime-culture-issue-when-passing-value-back-to-controller for details. – Tetsuya Yamamoto Jun 07 '17 at 08:19

0 Answers0