obeject에 대한 유효성 검사를 완료했으며이를 처리하는 가장 좋은 방법을 찾으려고합니다. xVal, 전체 클래스의 DataAnnotations
는 다음 클래스 감안할 때 :public class LetterResponse {
public Guid Id {get;set;}
public bool SendBlankCart {get;set;}
public string ToName {get;set;}
public string ToAddress {get;set;}
}
내가 그것을 지속하기 전에 클래스를 검증하기 위해 dataannotation 및 경우 xval를 사용하려면,하지만 두 개 이상의 속성을 필요로 복잡한 검증 있습니다.
의사 : 내가 할 매개 변수를 기대하고
public class LetterResponseValidator : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value.GetType() == typeof(LetterResponse))
{
var letterResponse = (letterResponse) value;
if (letterResponse.SendBlankCard)
{
return true;
} else
{
if (string.IsNullOrEmpty(letterResponse.FromDisplayName) || string.IsNullOrEmpty(letterResponse.ToAddress1))
{
return false;
}
return true;
}
}
return false;
}
}
:
[LetterResponseValidator]
public class LetterResponse {
public Guid Id {get;set;}
public bool SendBlankCart {get;set;}
public string ToName {get;set;}
public string ToAddress {get;set;}
}
과 같은 유효성 검사 규칙이 있습니다
if SendBlankCart {
- no validation on ToName, ToAddress
} else {
ToName - required.
ToAddress - required.
}
내가 이런 식으로 주석을 싶습니다 LetterResponse 클래스의 내 인스턴스이지만, 유효성 검사 러너에서 호출되지 않습니다.
누구든지이 문제를 해결할 방법을 알고 있습니까?
감사합니다,
핼
Yep - 내 DataAnnotation 러너 메서드는 속성 만 확인 중이며 클래스 수준의 특성은 아닙니다. 감사합니다. Hal – Hal
System.ComponentModel.DataAnnotations v 3.5 – Omu
에서 작동하는 DataAnnotationsModelBinder를 어디서 찾을 수 있는지 알고 계신가요? 일반 3.5는 위에서 링크 된 블로그 기사에서 설명한 버그를 제외하고 .NET 버전 3.5에서 작동합니다. . 이 기사에서는 버그를 수정하는 방법도 보여줍니다. –