2

在我的网络应用程序(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”,我必须输入日期。

这是视图

<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'。希望你能给我一些想法。

4

2 回答 2

1

您正在查看日期组件。但请记住,时间组件可以包括秒和小数秒,中间有一个句点。由于日期和时间经常组合在一起,如果句号被视为特殊字符,我不会感到惊讶。

更新:

我浏览了以下文档链:

DisplayFormatAttribute.ApplyFormatInEditMode 属性

DisplayFormatAttribute.DataFormatString 属性

格式化类型

自定义日期和时间格式字符串

我希望看到句点字符在格式字符串中具有特殊含义。相反,文档没有具体列出句点字符。这意味着它应该属于“任何其他字符”的类别,因此,“该字符被复制到结果字符串不变”。

不过,我不相信。小数秒出现在像 23:59:59.999 这样的时间分量中,我怀疑对句点字符进行了未记录的特殊处理。

于 2013-04-23T19:18:07.373 回答
1

格式的问题不在于 ASP 标记和字段,而在于不显眼的验证 jquery 库。我不得不为我的日期格式重载不显眼的 JavaScript 验证方法。更多详细信息在这里 MVC DateTime 验证 - 英国日期格式

于 2013-04-25T09:44:29.867 回答