2

I have this selfreferencing Model:

public class AddressDataViewModel
{
    [Required]
    public String Country {get; set;}

    public String Town {get; set;}

    public AddressDataViewModel AdditionalAddress {get; set;}
}

Problem is that the Required attribute is also applicated to the Country property of self referenced object AdditionalAddress and so on. Is there some easy way to suppress this? I only want Required validation to first of the hierarchy.

Thanks.

tereško
  • 58,060
  • 25
  • 98
  • 150
CoCumis
  • 99
  • 1
  • 8
  • Cause I have there many properties and I don't want to add the same properties more times only with some additional prefix (for example AdditionalTown, AdditionalCountry...) – CoCumis Dec 11 '13 at 16:33

1 Answers1

0

You could solve this with a base and derived class:

public abstract class AddressDataViewModel
{

    public virtual String Country {get; set;}

    public String Town {get; set;}

}

public class PrimaryAddressDataViewModel : AddressDataViewModel
{

    [Required]
    public Overrides String Country {get; set;}

}

public class AdditionalAddressDataViewModel : AddressDataViewModel
{
}

public class AddressesDataViewModel
{
     public PrimaryAddressDataViewModel PrimaryAddress {get;set;}
     IEnumerable<AdditionalAddressDataViewModel> AdditionalAddresses {get;set;}
}
Maess
  • 4,118
  • 20
  • 29