0

I have a DateTime field in my model that is shown in a form in my view like this:

<div class="editor-field">
    @Html.EditorFor(model => model.Age)
    @Html.ValidationMessageFor(model => model.Age)
</div>

When a new model is created and inserted into my database, I assign it a value of 09/11/1990 15:36:22. If I try to edit this model using my view and do not change the value of Age for my model, the model is validated and any changes to other fields are accepted and saved. However, if I change the Age field to 07/27/2010 15:36:22 I am getting a validation error" The value '07/27/2010 15:36:22' is not valid for Age. But, the date 09/12/2010 15:36:22 is validated successfully. Any reason why this is happening? In the debugger, the value returned for invalid dates is 01/01/0001.

Edit: I think this has to do with the culture for jquery-validate and my system, since the client side validation fails any date that is in DD/MM/YYYY format, while the server side validation is failing any date that is in MM/DD/YYYY format.

AlexC
  • 10,676
  • 4
  • 37
  • 55
Flood Gravemind
  • 3,773
  • 12
  • 47
  • 79
  • Sounds like it's a matter of the format of the date - dd/MM/yyyy vs MM/dd/yyyy. Does your `Age` property specify the format to use? (It seems odd to use this for `Age` in the first place - I'd expect it to be `DateOfBirth`, and then you probably don't need to worry about the time component, either.) – Jon Skeet Dec 03 '13 at 14:20
  • In fact the validator is right at this point, because '07/27/2010 15:36:22' is a pretty strange age ;-) – Marthijn Dec 03 '13 at 14:20
  • @JonSkeet Its to calculate when sth was posted. Its age of a post not person which is why I need the seconds field. – Flood Gravemind Dec 03 '13 at 14:25
  • 3
    @FloodGravemind: Okay, then I'd call it `PostingTimestamp` or something like that - it's not an actual age, which is a *length* of time (2 years or whatever). Naming is important :) That's aside from the actual problem, of course, but it's still worth looking at. – Jon Skeet Dec 03 '13 at 14:29
  • this might be of some help http://stackoverflow.com/a/14081487/1236044 – jbl Dec 03 '13 at 14:30
  • What date format do you want the system to use, dd/MM/yyyy or MM/dd/yyyy ? – AlexC Dec 03 '13 at 14:56
  • MM/dd/yyyy needs to be eradicated. – Phill Dec 03 '13 at 17:06

1 Answers1

0

Assuming you want to make the model work on the client side then I would change you ASP .NET Globalization settings to force the system to use the Globalization you want. This is only really a valid solution if you expect clients to use your system that use one consistent date time format.

The problem that you have encountered (I think) is that some browsers are not consistent about the globalization that they use client side and the globalization that they present to the wider world.

AlexC
  • 10,676
  • 4
  • 37
  • 55