Search code examples
asp.net-core-mvc.net-6.0

Problem binding DateOnly in ASP.NET Core 6 MVC


I was trying to use the new DateOnly type in .NET 6, but I had trouble with the model binding. A perfectly normal form data such as this:

enter image description here

was parsed to "0001.01.01" and accepted as valid by the binder.

Replacing all DataOnly with the good old DateTime instantly solved this issue.

Could this be a bug in the BCL or am I missing some intentions here? Is there any workaround except for implementing a custom model binder?


Solution

  • Full binding support for DateOnly and TimeOnly isn't available yet. Another related issue is this one. This is planned for .NET 7

    The workaround in the second issue is to create custom JsonConverter and TypeConverters. The creator of the second issue has packaged both in the DateOnlyTimeOnly.AspNet package