2010-05-29 3 views
1

내가 데이터는 ASP.NET MVC의 기본 유형을 검증

public abstract class Validated 
{ 
    public bool IsValid { get { return (GetRuleViolations().Count() == 0); } } 

    public abstract IEnumerable<RuleViolation> GetRuleViolations(); 
} 

public partial class User: Validated 
{ 
    public override IEnumerable<RuleViolation> GetRuleViolations() 
    { 
     if (this.Age < 1) 
      yield return new RuleViolation("Age can't be less than 1"); 
    } 
} 

그것은 잘 작동을 검증하기 위해 다음과 같은 클래스를 구현했습니다! 형태는 그냥

if (user.IsValid == false) blah... 

을 제출하지만 아직 나이가

int a = 0; 
if (!int.TryParse(age, out a)) 
{ 
      error = "Not integer"; 
      // ... 
} 

가 어떻게 내 모델이 이동할 수 정수는 것을 확인해야하는 경우?

+0

그래, 그게 내가 생각한거야. 별도로해야 할 것입니다. – Alex

답변