Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
 
[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
 
 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
 
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 5-Jan-12 19:37pm
Edited 5-Jan-12 22:40pm
v2
Comments
SAKryukov at 6-Jan-12 4:06am
   
What is the problem?
--SA
Sunasara Imdadhusen at 6-Jan-12 4:42am
   
I have updated my question.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi All,
 
I resolved my problem using following line of code.
 
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:
typeof(T).GetProperty(PeropertyName).GetValue(t,null)

SOLUTION

 
bool ValidationResult = Validator.TryValidateProperty(typeof(T).GetProperty(PeropertyName).GetValue(t,null),  new ValidationContext(t, null, null) { MemberName =  PeropertyName} , ValidationMessages);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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,
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 319
1 _Amy 310
2 Nirav Prabtani 263
3 Sergey Alexandrovich Kryukov 210
4 Richard MacCutchan 125
0 OriginalGriff 7,889
1 Sergey Alexandrovich Kryukov 7,006
2 Maciej Los 4,014
3 Peter Leow 3,708
4 CHill60 2,762


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 6 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100