查询字符串绑定在 RazorPage 上不起作用,即使 SupportsGet = true

Query string binding doesn't work on RazorPage even with SupportsGet = true

这是我的页面...

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();
        }
    }

这是查询字符串...

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

但是属性总是空的。

我错过了什么?

模型活页夹将与格式为 dd/MM/yyyy 或 MM/dd/yyyy 的日期值作斗争,它会在放弃和默默失败之前问自己。您应该将日期作为 yyyy-MM-dd 传递,理想情况下:

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

如果您无法控制日期格式,您可以自己解析这些值并将它们分配给 PageModel 属性。