具有服务器端验证的 MVC2 DataAnnotations
如何在不使用 MVC 库的情况下验证包含 DataAnnotations 的实体?当您在表示层中时,使用 Model.IsValid 没问题,但是当您想确保模型在域/业务层中有效时该怎么办?我是否需要一个单独的验证框架,或者我是否缺少一种简单的方法?
感谢您的帮助,
马克
How do you validate an entity containing DataAnnotations without using the MVC library? Using Model.IsValid is fine when you're within the Presentation layer, but what about when you want to ensure the model is valid in the Domain/Business layer? Do I need a separate validation framework, or is there an easy way I'm missing?
Thanks for any help,
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想你的意思是 ModelState.IsValid by Model.IsValid,对吧?好吧,DataAnnotions 根本不依赖于 MVC,因此您始终可以使用 IValidatableObject 接口。
或者验证器类可能更合适,通过使用 Validator.ValidateObject(object, ValidationContext)。
I suppose you mean ModelState.IsValid by Model.IsValid, right? Well, DataAnnotions don't depend on MVC at all, so you can always use the IValidatableObject interface.
Or perhaps the Validator class will be more appropriate, by using Validator.ValidateObject(object, ValidationContext).