Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 835
ZorgoZ Avatar asked Jan 26 '26 23:01

ZorgoZ


2 Answers

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

like image 169
Panagiotis Kanavos Avatar answered Jan 28 '26 13:01

Panagiotis Kanavos


As stated in the prior answer, full binding support is not available yet for DateOnly. However, I had a similar issue and was able to solve this using the DataType annotation.

I am working with an API that only requires the date and did not want to go through the hassle of getting the date from DateTime (albeit it's not much of a hassle, but still...).

This is the property on my model I use to build objects from the data I get back from the API:

[DataType(DataType.Date)]
public DateTime DateBecameLead { get; init; }

This is the property on my Index.cshtml.cs I use to filter the list of opportunity cards by date.:

[ BindProperty(SupportsGet = true), DataType(DataType.Date) ]
public DateTime DateBecameLead { get; init; } = new (2021, 1, 1);

Then, further down Index.cshtml.cs, I can easily compare the two properties:

OpportunityCards = OpportunityCards.Where(opp => opp.DateBecameLead >= DateBecameLead).ToList();

And for further context, here's the input tag on the Index.cshtml that is binded to the property from Index.cshtml.cs:

<input id="date-became-lead"
       type="date"
       asp-for="DateBecameLead" />

It's still using the old DateTime object, however, it does give the same benefit as using the DateOnly object and works just as expected. Once the full binding support is given to DateOnly, the code won't change much except I'll be able to live without the annotations.

like image 34
Justin Erdmier Avatar answered Jan 28 '26 13:01

Justin Erdmier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!