在我的网络应用程序(asp.new mvc)中,我尝试对日期字段进行验证。
如果我用这个
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? PeriodBeginFrom { get; set; }
或者这个
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
public DateTime? PeriodBeginFrom { get; set; }
它有效。但如果我尝试使用“.”来分隔分隔符
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
public DateTime? PeriodBeginFrom { get; set; }
我有一个问题。 jQuery 验证表明,如果我尝试“01.01.2001”,我必须输入日期。
这是 View
<div class="ElementDiv">
@Html.LabelFor(m => m.Filter.PeriodBeginFrom, new { @class = "Labels" })
@Html.EditorFor(m => m.Filter.PeriodBeginFrom, new { @class = "TextBoxes" })
@Html.ValidationMessageFor(m => m.Filter.PeriodBeginFrom, null, new { @class = "Errors" })
</div>
为什么?与所有其他分隔符一起工作。 “.”有什么问题?分隔符?
更新 1: 验证似乎无法正常工作。如果我的模型有这个属性
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? PeriodBeginFrom { get; set; }
我们将 DateTime 对象传递给模型,它将在文本框中显示为“.”就像“12.12.2012”而不是像我在等待“12/12/2012”。希望大家给我一些建议。
最佳答案
您正在查看日期组件。但请记住,时间分量可以包括秒和小数秒,中间有一个句点。由于日期和时间经常组合在一起,因此如果句点被视为特殊字符,我不会感到惊讶。
更新:
我浏览了以下文档链:
DisplayFormatAttribute.ApplyFormatInEditMode Property
DisplayFormatAttribute.DataFormatString Property
Custom Date and Time Format Strings
我希望看到句点字符在格式字符串中具有特殊含义。相反,文档没有具体列出句点字符。这意味着它应该属于“任何其他字符”类别,因此“该字符被原封不动地复制到结果字符串中。”
但是,我不相信。小数秒出现在像 23:59:59.999 这样的时间组件中,我怀疑对句点字符有未记录的特殊处理。
关于validation - 使用自定义格式的日期时间验证。 '.' 分隔符出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16176135/