Annotation Validation in Asp.net MVC just accept MM/dd/yyyy format

3.5k views Asked by At

I have spent several hours reading a lot of articles, but I still cannot solve my issue.

I have a DateTime type property in my model:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public System.DateTime StartTime { get; set; }

I have set the format to dd/MM/yyyy, so I expect a date like 14/12/2014 to be accepted. However, the validation always fails with the message: The field StartTime must be a date. I tried to input 12/14/2014 and it's ok.

Following several articles I have read, I tried to edit the format in razor code like:

@Html.TextBoxFor(model => model.StartTime, "{0:dd/MM/yyyy}")

And try using EditorFor like ataravati said in the below answer:

@Html.EditorFor(model => model.StartTime)

Or, change the culture in web.config to a culture in which day goes before month like GB:

<system.web>
    <globalization uiCulture="en" culture="en-GB"/>  
</system.web>

But, none of them work for me.

I tried using just a text input and input the date manually (not using any control like jqueryUI). However the validation still not accept my manually input if it's in dd/MM/yyyy format. Could anyone show me the issue or show me the way to figure it out what the issue is?

UPDATE: For someone who may have the same issue as me.

Following the post that Stephen mentions below, we have to create a 'date' validation to overwrite the default one of annotation validation. The custom validate we create will allow the format we want.

However, after that, the posted data to controller may not contain that data (in my case all datetime value is set to default datetime). The reason is the culture setting doesn't accept your format. We can change the culture setting in web.config but i don't think it's the best way because it can impact other value type (currency, number....) in your project. In my way, i create a custom ModelBinder for binding Datetime in the right format (an example here: http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder).

The above solution looks complicated. It shouldn't be so just for passing validation but up to now, i don't find anything better. Because of that, if someone have better solution that let annotation validation validates datetime value in format correctly, please share it.

2

There are 2 answers

1
ataravati On

The ApplyFormatInEditMode option only works when you use @Html.EditorFor, not @Html.TextBoxFor. Change your razor code to this:

@Html.EditorFor(model => model.StartTime)
0
Dav.id On

Actually one thing that has been a life saver for me is as follows, and you don't need any data annotations (unless you want them etc):

@Html.TextBoxFor(m => m.StartDate, new { @Value = Model.StartDate.ToString("dd-MMM-yyyy") })

The above format can of course be changed to whatever.

Of course a popular thing to do is add say a datepicker too. I am using jQuery UI datepicker for ease of use:

@Html.TextBoxFor(model => model.StartDate, new { @Value = Model.StartDate.ToString("dd-MMM-yyyy"), @class = "form-control datepicker" })

My view model class example is shown below for completeness:

[DisplayName("Start Date")]
[Required(ErrorMessage = "An start date must be supplied")]
public DateTime StartDate { get; set; }

Hope it helps.