2012-12-10 88 views
2

我是新来的MVC3 - 无法弄清楚为什么日期格式验证不能在客户端工作,即如果我手动更改文本框并输入格式日期无效。我在这个领域使用jQuery日期选择器。奇怪的是,必填字段验证正在工作。ASP.NET MVC3 - RAZOR - 日期格式验证不起作用

有人能让我知道这段代码有什么问题吗?

谢谢!

视图模型

[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] 
    [DataType(DataType.Date, ErrorMessage="Please enter a valid date in the format dd/mm/yyyy")] 
    [Required] 
    public DateTime ADate { get; set; } 

VIEW

 @Html.TextBox("ADate", Model.ADate.ToShortDateString(), new { @class = "ADate" }) 
     @Html.ValidationMessageFor(model => model.ADate, "*") 

回答

1

数据类型和DisplayFormat只有当使用DisplayFor或EditorFor,而不是与一个TextBox工作。但是,您无法使用EditorFor设置类名称。

1

您可以使用基本TextBoxFor()并指定一个类名

@Html.TextBoxFor(model => model.ADate, new { @class = "ADate" })