今天(2010 年 1 月 15 日)Scott 写了一篇关于 ASP.NET MVC2 模型验证的博客
http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
任何人都知道如何以编程方式在运行时添加验证规则?
“程序化注册”是ValidationAspects支持的类似功能
// register lambda syntax validation functions
typeof(User).GetProperty("Name").AddValidation<string>((name, context) =>
{ if (!Exists(name)) { throw new ValidationException("Username is unknown"); } } );
// register validation factories (classes)
typeof(User).GetProperty("Name").AddValidation(new [] { new NotNullOrEmpty()} );
// don't like strings?
TypeOf<User>.Property(user => user.Name).AddValidation(new [] { new NotNullOrEmpty()} );