日期字段上给予必要的验证错误 [英] Date field giving required error on validation

查看:85
本文介绍了日期字段上给予必要的验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net创建模型MVC 3网站,有一个名为DateOpened属性:

I have created a model in my asp.net MVC 3 website and have a property named DateOpened:

  [Column("Date Opened")]
        [Display(Name = "Date Opened:")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime DateOpened { get; set; }

我没有[必填]数据标注适用于它,但是当我尝试保存表单,它说必填字段。在数据库为null。

I didn't apply [Required] data annotation to it, but when I try to save the form, It says required field. In database it is null.

请提出解决方案。

推荐答案

这是正常的。日期时间是一个值类型,这意味着它总是需要一个值。在ASP.NET MVC模型元数据提供程序自动添加所需的属性非空的数据类型。你可以使用一个可为空的日期时间:

That's normal. DateTime is a value type meaning that it will always require a value. The model metadata provider in ASP.NET MVC automatically adds the required attribute to non-nullable data types. You could use a nullable DateTime:

[Column("Date Opened")]
[Display(Name = "Date Opened:")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? DateOpened { get; set; }

这篇关于日期字段上给予必要的验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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