I have a problem with an validation error that only appears on Windows Azure, but not on the local Azure Emulator.
In my model I have a class with an attribute “Start” and a DisplayFormat for the German date format:
[Required]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Beginn")]
public DateTime Start { get; set; }
On my local machine, everything is fine, but when I try to save the field on a Windows Azure instance I get this validation message:
The value ‘22.08.2011’ is not valid for Beginn.
Both (local and cloud) using the same database (Azure SQL).
So, I’m confused. Any idea how to fix this?
The
DisplayFormatattribute (as it name suggests) is used only for displaying the property value using the specified format. It has absolutely nothing to do with validation.In this case validation is performed by the default model binder when it tries to set the POSTed string to a DateTime property. It uses the default locale which could be specified in your
web.configusing the globalization element:The reason your code doesn’t work when you deploy it on the Azure instance is because chances are the server is configured to use the
en-USculture for which the date time format is different.If you want to use some more specific format you could also write a custom model binder for your view model and inside manually parse the POSTed value using this special custom format.