Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation problem with Windows Azure, EF and MVC3

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?

like image 654
Gesh Avatar asked Aug 20 '11 17:08

Gesh


1 Answers

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.

like image 151
Darin Dimitrov Avatar answered Oct 06 '22 00:10

Darin Dimitrov