ASP.Net 动态数据 - 数据类型验证
动态数据问题:
我的模型上有 2 个 Nullable
类型的字段
当我使用插入表单并输入垃圾字符串(例如日期的“sdfsdfas”)时,它会给我一条丑陋的错误消息
< em>无法将输入字符串“rtgh”转换为类型“System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]”。
所以我'我创建了一个元数据类,如下所示,以尝试获得更友好的错误
[MetadataType(typeof(RuleMetadata))]
public partial class Rule
{
public class RuleMetadata
{
[ScaffoldColumn(false)]
public Guid RuleId;
// tried this overload
[DataType("Some error")]
public Nullable<DateTime> ValidFrom;
// tried this overload
[DataType(DataType.Date)]
public Nullable<DateTime> ValidTo;
}
}
,但它完全被忽略,我得到了像以前一样丑陋的错误,
我做错了什么?
Dynamic Data question:
I have 2 fields of type Nullable<DateTime>
on my model
When I use the insert form and enter a garbage string e.g. "sdfsdfas" for a date it gives me an ugly error message
Unable to convert input string ' rtgh' to type 'System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.
So I've created a MetaData Class as follows to try and get a more friendly error
[MetadataType(typeof(RuleMetadata))]
public partial class Rule
{
public class RuleMetadata
{
[ScaffoldColumn(false)]
public Guid RuleId;
// tried this overload
[DataType("Some error")]
public Nullable<DateTime> ValidFrom;
// tried this overload
[DataType(DataType.Date)]
public Nullable<DateTime> ValidTo;
}
}
But it is totally ignored and I get the ugly error as before
what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终通过创建 CustomAttribute 来完成此操作
对于一个框架来说似乎有点矫枉过正,该框架旨在为您实现自动化,
然后将该属性应用到我的元数据类
eventually done this by creating a CustomAttribute
Seems like overkill for a framework which is meant to autmate stuff for you
then applied the attribute to my MetaData Class