0

I have a base class with some properties:

public class A
{
   [Display(Name = "Some Property")]
   public virtual int SomeProperty {get;set;}
}

and its derived class:

public class B:A
{
   [Required]
   public override int SomeProperty {get;set;}
}

But in fact, when I extract metadata for class B to ModelMetadata, IsRequired property is set to false. Is there a way to apply Required attribute, so it was reflected in ModelMetadata?

Alexander Smith
  • 369
  • 2
  • 16

1 Answers1

0

Please try the example below:

public class A
{
    [Display(Name = "Some Property")]
    public virtual int SomeProperty { get; set; }
}
public class B : A
{
    [MyRequired]
    public override int SomeProperty { get; set; }
}

[AttributeUsage(AttributeTargets.Property, Inherited = true)]

public class MyRequiredAttribute : RequiredAttribute
{

}

finally:

test of code

Ahmad Javadi Nezhad
  • 527
  • 1
  • 5
  • 23