Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
UPDATED: When i executing Unit Test project and then it will return Was unhandled This test result also contained an inner exception instead of "Assert.IsTrue failed. The Description field is required." Result like (0 Pass, 1 FAIL, 1 Total) but we are not getting any exception at all if I debug with F11

C#
[TestMethod]
[Asynchronous]
[Description("Determines whether the selected or single property is valide using the validation context or validate single properties.")]
        public void ValidateSigleWithDataAnnotation()
        {
            LookupsServices lookupsservices = new LookupsServices();
            Lookups lookups = new Lookups() { Description = "", LookupReference = 2, DisplayOrder = 50};
            lookupsservices.Lookups.Add(lookups);

            //THIS IS NOT WORKING
            string message = ValidateProperties.ValidateSingle(lookups, "Description");
            Assert.IsTrue(message.Equals(""), message);
            //THIS IS WORKING
            var results = new List<ValidationResult>();
            Validator.TryValidateProperty(lookups.Description , new ValidationContext(lookups, null, null) { MemberName = "Description" }, results);
            Assert.IsTrue(results.Count == 0, results[0].ToString());
        }


Following is the Generic function to validate individual property

C#
public static string ValidateSingle<T>(T t, string PeropertyName) where T : class
       {
           string errorMessage = "";
           var ValidationMessages = new List<ValidationResult>();
           bool ValidationResult = Validator.TryValidateProperty(typeof(T).GetProperty(PeropertyName).Name,  new ValidationContext(t, null, null) { MemberName =  PeropertyName} , ValidationMessages);

           if (!ValidationResult) errorMessage += string.Format("\n{0}", ValidationMessages[0]);
           return errorMessage;
       }


Following is the Model where Description field id Required

C#
public class Lookups
    {
        public Lookups() { }
        [Key]
        public virtual int LookupReference { get; set; }
        [Required]
        public virtual string Description { get; set; }
        public virtual int? DisplayOrder { get; set; }
    }


I am getting error "The Description field is required" if I am validating without Generic method, but why am not getting same error using Generic method?
Posted
Updated 5-Jan-12 22:40pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Jan-12 4:06am    
What is the problem?
--SA
Sunasara Imdadhusen 6-Jan-12 4:42am    
I have updated my question.

Hi All,

I resolved my problem using following line of code.

C#
bool ValidationResult = Validator.TryValidateProperty(typeof(T).GetProperty(PeropertyName).Name,  new ValidationContext(t, null, null) { MemberName =  PeropertyName} , ValidationMessages);


In the first form, I am passing in the name of the property, i.e. "Description". In the second form, I am passing in the value of the property, i.e. "". To make the first call look like the second:
C#
typeof(T).GetProperty(PeropertyName).GetValue(t,null)

SOLUTION


C#
bool ValidationResult = Validator.TryValidateProperty(typeof(T).GetProperty(PeropertyName).GetValue(t,null),  new ValidationContext(t, null, null) { MemberName =  PeropertyName} , ValidationMessages);
 
Share this answer
 
Hi,

I think you could not arrived to what
you are asserting as false as you are expecting, because you defined Description = "". Your assertion always return true as what you have defined above...

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900