简体   繁体   English

如何使 DateTime 不需要?

[英]How do I make DateTime not required?

I have this model for an app that tracks our migration status for users.我有一个用于跟踪用户迁移状态的应用程序的模型。 If they haven't been migrated, there's no migration date.如果它们尚未迁移,则没有迁移日期。 So, how can they leave that field blank?那么,他们怎么能将该字段留空呢? It's not annotated as required, yet validation fails as if it is required.它没有按要求进行注释,但验证失败了,就好像它是必需的一样。 I cannot seem to set it to null either.我似乎也无法将其设置为 null。 How does one get around this problem?如何解决这个问题?

[Display(Name = "Migrated?")]
public bool migrated { get; set; }

[Display(Name = "Date Migrated")]
public DateTime migrationdate { get; set; }

Use a nullable type使用可空类型

[Display(Name = "Migrated?")]
public bool migrated { get; set; }

[Display(Name = "Date Migrated")]
public DateTime? migrationdate { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM