MVC DataAnnotation 在 MVC3 中渲染错误的 HTML
我有一个简单的模型对象,如下所示:
[DataMember]
[DataType(DataType.Date)]
public DateTime WeekEndingDate { get; set; }
和一个执行此操作的视图页面,
@Html.TextBoxFor(model => model.CashEntry.WeekEndingDate)
但我的问题是它将其呈现为标记:
<input data-val="true" data-val-required="The WeekEndingDate field is required." id="CashEntry_WeekEndingDate" name="CashEntry.WeekEndingDate" type="text" value="" />
当真正的 data-val-required 应该是 data-val-date 时。这也搞砸了日期字段页面上的验证。对于为什么会发生这种情况有什么想法吗?
I have a straight forward model object that looks like this:
[DataMember]
[DataType(DataType.Date)]
public DateTime WeekEndingDate { get; set; }
And a View Page That Does this
@Html.TextBoxFor(model => model.CashEntry.WeekEndingDate)
But My Problem is that it renders this as the markup:
<input data-val="true" data-val-required="The WeekEndingDate field is required." id="CashEntry_WeekEndingDate" name="CashEntry.WeekEndingDate" type="text" value="" />
When really data-val-required should be data-val-date . This is screwing up the validation also on the page of the date field. Any thoughts as to why this may be happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我担心 ASP.NET MVC 3 永远不会创建 data-val-date,这里是可能创建的属性列表:
您可以为 DataType 属性创建额外的 ModelValidatorProvider,如下所示:
并在 Global.asax 中注册它,如下所示:
这应该为您提供对 DataType.Date 的客户端验证支持,但您必须记住它仅适用于 jQuery 验证,因为 Microsoft 验证没有日期规则。
I'm affraid ASP.NET MVC 3 never creates data-val-date, here is list of attributes that might be created:
You can create additional ModelValidatorProvider for DataType attribute like this:
And register it in your Global.asax like this:
This should give you client validation support for DataType.Date, but you must remember that it will work only with jQuery validation as Microsoft validation doesn't have rule for date.