MVC 4实体框架6.1 DataAnnotationValidator错误日期

时间:2014-04-02 21:33:36

标签: c# asp.net-mvc-4 data-annotations entity-framework-6.1

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

我在使用和不使用[DataType(DataType.DateTime)]时尝试过它。我已经尝试过没有成功的RegularExpression。我肯定错过了什么。任何帮助表示赞赏。谢谢。

 [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; }
 }

更新:找到此solution。修改如下。虽然不完全是我想做的,但到目前为止效果还不错。我只需要在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>

0 个答案:

没有答案
相关问题