我有这种自我引用模型:
public class AddressDataViewModel
{
[Required]
public String Country {get; set;}
public String Town {get; set;}
public AddressDataViewModel AdditionalAddress {get; set;}
}
问题是Required属性也适用于自引用对象AdditionalAddress的Country属性,依此类推。 是否有一些简单的方法来抑制这种情况?我只希望对层次结构的第一个进行必要的验证。
感谢。
答案 0 :(得分:0)
您可以使用基类和派生类来解决此问题:
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;}
}