DateTime数据注释在ASP.NET Core RC2应用程序中不起作用 [英] DateTime Data Annnotations do not work in ASP.NET Core RC2 application

查看:72
本文介绍了DateTime数据注释在ASP.NET Core RC2应用程序中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

if I add Data annotations to my ViewModel the value of the DateTime field is always {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; }
}

如果我删除数据注释并使用默认值,则控制器非常简单.

The controller is very straightforward, if I remove the data annotation and use the default everything works fine.

控制器

[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 Core MVC RC2应用程序-也许我在那里丢失了一些东西.

It is an ASP.NET Core MVC RC2 application - perhaps I'm missing something there.

推荐答案

在模型中将DateTime更改为DateTime?.

这篇关于DateTime数据注释在ASP.NET Core RC2应用程序中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆