2

Here's my page...

public class MyPage : PageModel
    {
        [BindProperty(SupportsGet = true)]
        public DateTime? StartDate { get; set; }

        [BindProperty(SupportsGet = true)]
        public DateTime? EndDate { get; set; }

        public async Task<IActionResult> OnGetAsync()
        {
            if (ModelState.IsValid)
            {
               await Task.Delay(1);
            }

            return Page();
        }
    }

This is the query string...

?StartDate=29%2F10%2F2018&EndDate=31%2F10%2F2018

But the properties are always null.

What am I missing?

Ian Warburton
  • 15,170
  • 23
  • 107
  • 189

1 Answers1

2

The model binder will struggle with date values formatted as dd/MM/yyyy, or is that MM/dd/yyyy, it asks itself before giving up and failing silently. You should pass the dates as yyyy-MM-dd, ideally:

?StartDate=2018-10-29&EndDate=2018-10-31

If you have no control over the date format, you can parse the values yourself and assign them to the PageModel properties.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • Incidentally, how would one have a regular HTML form using jQuery datepicker submit a fancy date format? – Ian Warburton Oct 19 '18 at 18:47
  • @IanWarburton You can set the format of the datepicker using one of its options: https://api.jqueryui.com/datepicker/#option-dateFormat. However, these days. modern browsers render a datepicker if you set the input type to `date` (or use a tag helper) and take care of that for you. – Mike Brind Oct 19 '18 at 19:02
  • I want my datepicker date format to be different to the format that .net expects. Anyhow, I've manually parsed the string. Chrome's datepicker apparently can't be styled. https://stackoverflow.com/a/48509141/221683 – Ian Warburton Oct 19 '18 at 19:09