Reputation: 1883
I cannot get Validator to use DataAnnotations in my model to validate an object.
Here is my model property that I am trying to validate:
[MaxLength(350)]
public string SubSessionName { get; set; }
Here is the code I am using to validate it:
//validate the subsession
var validationContext = new ValidationContext(subSession, serviceProvider: null, items: null);
var validationErrorsList = new List<ValidationResult>();
var subsessionIsValid = Validator.TryValidateObject(subSession, validationContext, validationErrorsList);
Even though the SubSessionName property has a length of 401 the Validator still returns true--it should return false. Any ideas on what I might not be wiring up correctly?
Upvotes: 1
Views: 89
Reputation: 2745
Wrong TryValidationObject overload. Try this one.
var subsessionIsValid = Validator.TryValidateObject(
subSession, validationContext, validationErrorsList, true);
Upvotes: 1