Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top