title: Custom Validation Attribute MVC2
body: <p>I have a custom validation attribute which checks to see if two properties have the same values or not (like password and retype password):</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
      public class EqualToPropertyAttribute : ValidationAttribute
      {
          public string CompareProperty { get; set; }
  
          public EqualToPropertyAttribute(string compareProperty)
          {
              CompareProperty = compareProperty;
              ErrorMessage = string.Format(Messages.EqualToError, compareProperty);
          }
  
          public override bool IsValid(object value)
          {
              if (value == null)
              {
                  return true;
              }
              PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
              var property = properties.Find(CompareProperty, true);
              var comparePropertyValue = property.GetValue(value).ToString();
  
              return comparePropertyValue == value.ToString();
          }
      }
  </code></pre>
  
  <p>I have a view model class which has all the fields for the signup form as follows:</p>
  
  <pre><code>public class SignUpViewModel
      {
          [Required]
          [StringLength(100)]
          public string Username { get; set; }
  
          [Required]
          [Password]
          public string Password { get; set; }
  
          [Required]
          [DisplayText("RetypePassword")]
          [EqualToProperty("Password")]
          public string RetypePassword { get; set; }
  
          [Required]
          [StringLength(50)]
          [DisplayText("FirstName")]
          public string FirstName { get; set; }
  
          [Required]
          [StringLength(100)]
          [DisplayText("LastName")]
          public string LastName { get; set; }
  
          [Required]
          [DisplayText("SecurityQuestion")]
          public int SecurityQuestionID { get; set; }
  
          public IEnumerable&lt;SelectListItem&gt; SecurityQuestions { get; set; }
  
          [Required]
          [StringLength(50)]
          public string Answer { get; set; }
  }
  </code></pre>
  
  <p>Below is my controller code:</p>
  
  <pre><code>public virtual ActionResult Index()
      {
          var signUpViewModel = new SignUpViewModel();
  
          signUpViewModel.SecurityQuestions = new SelectList(questionRepository.GetAll(),"SecurityQuestionID", "Question");
          return View(signUpViewModel);
      }
  
          [HttpPost]
          public virtual ActionResult Index(SignUpViewModel viewModel)
          {
              // Code to save values to database
          }
  </code></pre>
  
  <p>When I enter the form values and hit submit the line of code which tries to get the property descriptor <strong>var property = properties.Find(CompareProperty, true);</strong> is returning null. Can anyone help me understand why this is happening?</p>