日期时间数据注释在 ASP.NET 核心 RC2 应用程序中不起作用

DateTime Data Annnotations do not work in ASP.NET Core RC2 application

如果我向我的 ViewModel 添加数据注释,则 DateTime 字段的值始终为 {1/1/0001 12:00:00 AM}。

ViewModel

public class EditReleaseViewModel : BaseViewModel
{
        [Display(Name = "ReleaseDate", ResourceType = typeof(SharedResource))]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy HH:mm}")]
        [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(SharedResource))]
        public DateTime ReleaseDate { get; set; }
}

控制器非常简单,如果我删除数据注释并使用默认值一切正常。

控制器

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(EditReleaseViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                ...
            }
        }

查看

<div class="form-group col-sm-12 col-md-6 col-lg-4">
            <label asp-for="ReleaseDate" class="col-md-3 control-label"></label>
            <div class="col-md-9">
                <input asp-for="ReleaseDate" class="form-control" />
                <span asp-validation-for="ReleaseDate" class="text-danger" />
            </div>
        </div>

它是一个 ASP.NET 核心 MVC RC2 应用程序 - 也许我在那里遗漏了一些东西。

将模型中的 DateTime 更改为 DateTime?

我自己想出来的:

添加另一个数据注释 [DataType(DataType.Date)],然后 'Build' 项目,然后一切正常。

我不知道方法,但它对我有用。

顺便说一句,我正在按照这里的教程进行操作:https://docs.asp.net/en/latest/tutorials/first-mvc-app/adding-model.html