lbrahim
lbrahim

Reputation: 3810

DateTime Format In Model Binding

In my Web API project I have following model:

public class ModelClass
{
    [Required(ErrorMessage = "Date Cannot Be Empty", AllowEmptyStrings = false)]
    [DataType(DataType.Date, ErrorMessage="Date Not In Correct Date Format")]
    public DateTime date { get; set; }
}

which is being utilized in following action:

public HttpResponseMessage Submit([FromUri] ModelClass model)
{
 //do sth
}

Now if I pass date like localhost:3647/api/Controller/Submit?date=31/12/2015 ModelState validation fails for DataType even though I changed globalization to match the date format of dd/MM/yyyy like below:

<globalization culture="ms-MY" uiCulture="ms-MY" />

Upvotes: 1

Views: 3515

Answers (1)

Davide Icardi
Davide Icardi

Reputation: 12229

In ASP.NET MVC as I known query string are always parsed using InvariantCulture. Only POST body are parsed using Thread Culture. I think that this is to allow to create urls that are valid for all users.

Look also at this answer: https://stackoverflow.com/a/9977290/209727

Or this blog post: http://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization

If you really need to use this format maybe you can just use a string and parse it inside your controller?

Upvotes: 3

Related Questions