【发布时间】:2011-04-03 03:37:51
【问题描述】:
我的团队正在使用 MetadataType 中的模型验证构建 ViewModel。我的问题是我使用的是非 MVC 项目,我可以用它来验证模型吗?如果是的话,你能举个例子吗?
[MetadataType(typeof(PersonMetadata))]
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
}
public class PersonMetadata
{
[StringLength(255, ErrorMessage="Name is required"), Required]
[DisplayName("Name")]
public string Name { get; set; }
}
提前谢谢你!
【问题讨论】:
-
+1 评论、有用的链接,但最后你认为使用元数据或 Fluent 验证器如何?
-
@M.Radwan 我现在使用了 FluentValidation,它看起来比 MetaData 更简洁易用。
标签: c# asp.net asp.net-mvc-3