我的ASP.NET Core 3.0项目中具有以下模型:
class DataModel
{
public DateTime StartDate {get; set;}
public DateTime EndDate {get; set;}
}
我使用以下控制器代码:
[HttpGet]
public IActionResult Index(DataModel dataModel = null)
{
actualDataModel = new DataModel { StartDate = DateTime.Today.AddDays(-7), EndDate = DateTime.Today };
if (dataModel.StartDate != DateTime.MinValue)
{
actualDataModel.StartDate = dataModel.StartDate;
actualDataModel.EndDate = dataModel.EndDate;
}
return View(actualDataModel);
}
在视图中,我具有以下字段:
<input asp-for="StartDate" class="form-control" asp-format="{0:yyyy-MM-dd}" type="date" />
第一次运行时,我会看到正确的日期,这就是来源的样子:
<input class="form-control" type="date" data-val="true" data-val-required="The StartDate field is required." id="StartDate" name="StartDate" value="2019-10-30" />
刷新后,我会得到空的视野,其中包含以下来源:
<input class="form-control" type="date" data-val="true" data-val-required="The StartDate field is required." id="StartDate" name="StartDate" value="10/30/2019 00:00:00" />
为什么ASP.NET无法正确格式化日期?我该如何强迫?