问题
I have a (likely common) case where I would like to reuse the same class for both adding a new entity and editing an existing entity in my silverlight app (SL4, WCF RIA, EF4, Linq-to-Entities). When adding a new one, I would like my custom validator to check for uniqueness and fail if it's not unique. However, when editing, I don't want that same validator to prevent edits. Is there a way to temporarily turn off validation for a single field (ideally by validation type so that the other validators can still run on that same field)?
回答1:
You might want to check out the ValidationContext capabilties here. You can have validation attributes that make decisions based on properties on the entity, or any other state you want to provide.
Check out my blog posts on Providing ValidationContext and Using ValidationContext:
http://jeffhandley.com/archive/2010/10/25/RiaServicesValidationContext.aspx
http://jeffhandley.com/archive/2010/10/25/CrossEntityValidation.aspx
Hope this helps,
Jeff
来源:https://stackoverflow.com/questions/4125014/is-there-a-way-to-selectively-skip-silverlight-field-validation-when-using-wcf-r