0

我有一个使用 DataAnnotationValidator.cs 作为验证的一站式类的 EF 模型。我喜欢这种方法,但日期数据类型除外。我发现了很多关于验证除日期之外的所有内容的信息。您如何使用这种技术验证日期?以下正确显示日期,但允许任何格式正确的输入作为有效日期:32/32/2014 通过测试,但 32//2014 没有。

我已经尝试过使用和不使用 [DataType(DataType.DateTime)]。我试过正则表达式没有成功。我肯定错过了什么。任何帮助表示赞赏。谢谢你。

 [MetadataType(typeof(v_Demographics_Metadata))]
 public partial class v_Demographics
 {
 }
 class v_Demographics_Metadata
 {
 [Display(Name = "Date of Birth"), DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
 [DataType(DataType.DateTime)]
 public Nullable<System.DateTime> DateOfBirth { get; set; }
 }

更新:找到这个解决方案。修改如下。虽然不完全是我想做的,但到目前为止效果很好。我只需要在 jQuery 中添加一些验证

在 \Shared\EditorTemplates 中创建了一个 DateTime.cshtml,其中包含以下内容:

 @model System.DateTime?
 @Html.TextBox("", String.Format("{0:d}", Model.HasValue ? Model : DateTime.Today), new { @class = "dp" })

我将以下 jQuery 添加到我的 DemographicsView。我计划在 _Layout.cshtml 中测试 jQuery 以了解它如何影响整个站点。

<script type="text/javascript">
$(document).ready(function () {
    $('.dp').datepicker({
        changeMonth: true,
        changeYear: true,
        showOn: "button",
        buttonImage: "/Images/calendar.gif",
        buttonImageOnly: true
    });
});
</script>
4

0 回答 0