'Razor Pages form date saved as 0001-01-01

I've been struggling with this for a couple of hours now and I'm getting nowhere. I've created a basic Razor Pages site with a form and connected it to a MySql database by following the ms tutorial (https://docs.microsoft.com/de-de/aspnet/core/tutorials/razor-pages/?view=aspnetcore-6.0).

Everything is working fine except for the date picker, that's not behaving correctly at all. First off it's displaying the date as mm/dd/yyyy even though the current culture is de-DE so the correct display should be dd.mm.yyyy. I've even tried setting that manually with DataFormatString but it still shows the date as mm/dd/yyyy. That's not an issue per se but to me it already indicates that something's not working as intended here.

Trying to save the values to the databse is where the real fun begins, though. As hinted at in the title, every date I enter is saved as 0001-01-01 in my MySql database, i.e. the default value. When I tried to narrow down the issue I realized that the value isn't being saved to the variable.

I first opened the dev tools in chrome and opened the network tab to see if the the correct value is being passed on and it appears to be. In the payload part of the submitted form the correct date is displayed. I then tried outputting the variable to the console from the OnPostAsync method of my form. The output there however is not the correct date but 01.01.0001, which appears to be the German date format btw. The other form fields are being output correctly.

So yeah, that's where I'm at. I believe the issue to be with the html date picker but I don't know what I can do about it. Edit: Actually, the issue can't really be with the date picker(even though it's behaving strangely) since the form seems to be submitting the correct date.

I am aware btw that I need to change the date format before passing it to my database, I'm going to take care of that once this issue is solved.

I've tried to condense the code down to the relevant parts, let me know if anything important is missing.

Create.cshtml

        <div class="row pl-0 pt-3">
            <div class="col-sm-2">
                <label asp-for="NewEmployee.EntryDate" class="form-label">Eintrittsdatum</label>
                <input type="date" asp-for="NewEmployee.EntryDate" class="form-control">
            </div>

Create.cshtml.cs

public class CreateModel : PageModel
{
    private readonly PersonaleinstellungContext _context;

    public CreateModel(PersonaleinstellungContext context)
    {
        _context = context;
    }

    public IActionResult OnGet()
    {
        return Page();
    }

    [BindProperty]
    public Employee NewEmployee { get; set; } = new();

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        Console.WriteLine(NewEmployee.EntryDate);

        _context.Employee.Add(NewEmployee);
        await _context.SaveChangesAsync();

        return RedirectToPage("./Index");
    }
}

model

    [Display(Name = "Eintrittsdatum")]
    [DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
    [Required]
    public DateOnly EntryDate { get; set; }

data

public class PersonaleinstellungContext : DbContext
{
    public PersonaleinstellungContext(DbContextOptions<PersonaleinstellungContext> options)
        : base(options)
    {
    }

    public DbSet<Personaleinstellung.Models.Employee> Employee { get; set; }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source