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 DisplayFormat
attribute (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.config
using the globalization element:
<system.web>
...
<globalization culture="de-DE" uiCulture="de-DE"/>
</system.web>
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-US
culture 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With