简体   繁体   English

Html.EditorFor(DateTime)防止使其成为可选字段

[英]Html.EditorFor(DateTime) prevents making it an optional field

My model has a property like this: 我的模型具有以下属性:

[Display(Name = "Date of Service")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] // ISO compliance for jQuery validation
[SqlDateRange] // Enforces min/max SQL DateTime values (also handles nulls caused by invalid dates like "12/01/19900" -- I suppose the issue could be resolved by finding a better way of handling that null case
public DateTime? DateOfService { get; set; }

But when I use this in my view, 但是当我使用它时,

@Html.EditorFor(model => model.Date)

I get this , which gets passed as null. 我得到这个 ,它作为null传递。 I'm already checking for null via an attribute because the editor also allows hugely invalid dates like 12/12/21390 -- which also pass as null . 我已经在通过属性检查null了,因为编辑器还允许使用12/12/21390这样的无效日期,这些日期作为null传递。

Edit: I'm using Chrome. 编辑:我正在使用Chrome。

All you have to do is type format you want in html. 您要做的就是在html中键入所需的格式。 I hope it will work for you. 希望它对您有用。

@Html.EditorFor(model => model.Date, "{0:dd/MM/yyyy}")

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

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