.NET:DataAnnotation 属性概述
ASP.NET MVC 2 将支持基于 DataAnnotation 属性的验证,如下所示:
public class User
{
[Required]
[StringLength(200)]
public string Name { get; set; }
}
How can I check that a current model state is valid using only pure .NET (not using MVC Binding 、控制器方法等)?
理想情况下,这将是一个单一的方法:
bool IsValid(object model);
ASP.NET MVC 2 will support validation based on DataAnnotation attributes like this:
public class User
{
[Required]
[StringLength(200)]
public string Name { get; set; }
}
How can I check that a current model state is valid using only pure .NET (not using MVC binding, controller methods, etc.)?
Ideally, it would be a single method:
bool IsValid(object model);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码示例来自 Steve Sanderson 的 博客 关于 xVal (它使用 DataAnnotationsAttribute 来验证属性)。基本上,您只需要使用反射枚举属性并检查 IsValid():。
This code sample is from Steve Sanderson's blog about xVal (which uses the DataAnnotationsAttribute to validate properties). Basically, you just need to enumerate the attibutes using reflection and check IsValid():.