今天写一个验证功能的代码 , 用IValidatableObject接口的“自我验证” 来实现 ,该接口定义在“System.ComponentModel.DataAnnotations”命名空间下。
却发现using了这个类却无法实现接口,提示是没有引用。这让我很头疼 ,我用的是mvc4 。后来终于解决了
解决方案
您必须引用在此命名空间中定义的组件(它不是由默认在Visual Studio模板参考)。打开你的参考经理和添加到System.ComponentModel.DataAnnotations集的引用(解决方案资源管理 - >添加引用 - >选择.NET选项卡 - >从列表中选择System.ComponentModel.DataAnnotations)
另附四种验证方法
https://www.cnblogs.com/artech/p/asp-net-mvc-validation-programming.html