Trouble Implementing Custom Validation Attribute in ASP.NET Core MVC for Complex Objects
Could someone explain I'm trying to create a custom validation attribute in my ASP.NET Core MVC application to validate a complex object. The attribute should check if specific properties within the object meet certain criteria, but I'm working with an scenario where the attribute is not being triggered during model validation. I'm using ASP.NET Core 6 and my custom attribute looks like this: ```csharp public class CustomComplexValidationAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var model = value as MyComplexModel; if (model == null) { return new ValidationResult("Model want to be null."); } if (string.IsNullOrEmpty(model.PropertyOne) && model.PropertyTwo < 10) { return new ValidationResult("Either PropertyOne must be set or PropertyTwo must be at least 10."); } return ValidationResult.Success; } } ``` I've applied this attribute to my model: ```csharp public class MyComplexModel { [CustomComplexValidation] public string PropertyOne { get; set; } public int PropertyTwo { get; set; } } ``` However, when I submit the form without setting either property, I don't see any validation behavior messages related to my custom attribute. I've made sure that the model state is being validated in my controller action: ```csharp public IActionResult Submit(MyComplexModel model) { if (!ModelState.IsValid) { return View(model); } // Process the valid model return RedirectToAction("Index"); } ``` I've also tried explicitly calling `Validator.TryValidateObject(model, context, validationResults, true)`, but it still doesn't seem to trigger the custom validation logic. Is there something I'm missing, or is there a specific setup step that I need to ensure is in place for custom validation attributes to work properly in ASP.NET Core? Any insights would be greatly appreciated! Has anyone else encountered this? I'm developing on Ubuntu 20.04 with C#. Has anyone dealt with something similar? I'm working on a application that needs to handle this. Thanks for your help in advance! I'm developing on Windows 10 with C#. Any advice would be much appreciated.