0

I have the following entities for example:

public class BaseClass
{
    [Required]
    public virtual string DisplayName { get; set; }
}

public class FirstChildClass: BaseClass
{
    public int Id { get; set; }
}

public class SecondChildClas: BaseClass
{
   public int Id { get; set; }
}

Then i have the following viewmodel:

public class MyViewModel
{
    public FirstChildClass FirstProperty { get; set; }
    public SecondChildClass SecondProperty { get; set; }
}

In my View i have a form which submits the following properties FirstChildClass.Id and SecondChildClass.Id.

My problem is that the inherited DisplayName is added to my ModelState due to the [Required] attribute. I'd like to ignore the validation of inherited properties without removing them explicitly from the ModelState with Remove().

Is there any way to accomplish it?

janw
  • 8,758
  • 11
  • 40
  • 62
  • Maybe it will helps you https://stackoverflow.com/questions/15957495/how-do-i-stop-the-required-annotation-from-being-inherited-on-overridden-field – Alexander May 28 '20 at 10:49

1 Answers1

0

I would suggest not using inheritance here. What is BaseClass:

I have DisplayName and it should be Required but not always, it depends, looks in all derived classes to understand how I work.

If you really want to go that way check if you can make this property virtual, and add attribute only to one derived class.