I’m using a base contact model which other custom contact models classes inherit.
public class BaseContactModel
{
[Required(ErrorMessage = "Firstname is required")]
public virtual string FirstName { get; set; }
}
The base contact model uses validation attributes to flag a property is required but in some cases I want to override or stop that. Is this going to be possible?
public class ContactModel : BaseContactModel
{
[NotRequired]
public override string FirstName { get; set; }
}
I attempted to use a new validation attribute NotRequired to just return true, but appears the attributes are just being stacked up so Required & NotRequired are running and the validation is failing.
On looking for solutions (which I couldn’t find) I found that some unrelated attributes have an ‘inherited’ property, but I don’t see this in the native validation attributes in System.ComponentModel.DataAnnotations.
Is this a lost cause? Do I need to roll my own versions which would support disabling inheritance? Any help greatly appreciated.
See this below, I’ve created one class
Modelthat inherits from anotherBaseModel, used the new keyword then validated one of each instance. From what I can see, they both use the base attributes.I’ve added a control class
ControlModelfor clarity on the validation routine.ConsoleApplication1.Model test: isValid = False
ConsoleApplication1.BaseModel test: isValid = False
ConsoleApplication1.ControlModel test: isValid = True
From this example, it appears you can’t override/hide/ignore inherited required validation (haven’t tried others yet) attributes.