I'm using ASP.NET Core 6 MVC to create an app linked to a database and I have an issue :
I scaffolded a controller and its views (create, edit...) but when I try to create an object to send to db with Entity Framework, the DateOnly typed fields won't work and put in this date for all dates I try to write : 292269055-12-03 (it seems to be the max value ?).
I need to use DateOnly type for compatibility reasons because the database has date columns (I found this information here https://www.npgsql.org/doc/types/datetime.html) and if I try to use datetime, it doesn't work.
There is my code :
View :
<div class="form-group">
<label asp-for="BirthDay" class="control-label"></label>
<input asp-for="BirthDayUI" class="form-control" type="date"/>
<span asp-validation-for="BirthDay" class="text-danger"></span>
</div>
Controller (autogenerated) :
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,FirstName,BirthDay,Email,MobilePhone,LandLinePhone,PostalCity,PostalCode,PostalAdress,Gender,MedicalCertificate,MedicalCertificateStart,MedicalCertificateEnd,ProfilePicture")] Member member)
{
if (ModelState.IsValid)
{
_context.Add(member);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(member);
}
And finally, the model where I try to fiddle this :
[Required]
public DateOnly BirthDay { get; set; }
[NotMapped]
public DateTime BirthDayUI
{
get => BirthDay.ToDateTime(new TimeOnly());
set => BirthDay = DateOnly.FromDateTime(value);
}
The getter works perfectly but not the setter and I don't know what I must change.
Thank you for helping me