Można użyć do tego niestandardowego atrybutu. W skrócie, atrybut niestandardowy pobierze obie wartości, a następnie upewni się, że co najmniej jeden ma wartość. Aby uzyskać więcej informacji, patrz this page. Oto przykład (kod niesprawdzone):
[AttributeUsage(AttributeTargets.Property, AllowMultiple =false, Inherited = false)]
public class ValidatePersonName: ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string FirstName = (string)validationContext.ObjectType.GetProperty("FirstName").GetValue(validationContext.ObjectInstance, null);
string LastName = (string)validationContext.ObjectType.GetProperty("LastName").GetValue(validationContext.ObjectInstance, null);
//check at least one has a value
if (string.IsNullOrEmpty(FirstName) && string.IsNullOrEmpty(LastName))
return new ValidationResult("At least one is required!!");
return ValidationResult.Success;
}
}
Zastosowanie:
class Person{
[ValidatePersonName]
FirstName{get;set;}
LastName{get;set;}
}
Nie wiem, ASP.NET, ale jeśli tej pracy jako doktryny w PHP, powinieneś mieć imprezę dla prepersist dane teleadresowe na Baza danych. Powinieneś utworzyć warunek na tym zdarzeniu, który zgłosi błąd, jeśli obie wartości są zerowe? –