2012-12-16 4 views
0

제목이 조금 이상하게 보입니다.actionfilter의 속성에서 값을 얻는 방법

음 Asp.net MVC 3에서 새로 도입되었습니다. 이름이 국가 ID 번호 인 내 속성에 대한 속성을 만들고 싶습니다.

public class IdentityNumberControl : ActionFilterAttribute 
{ 
    public string WrongIdentityNumber { get; set; } 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 

     if(???) 
     { 
      filterContext.HttpContext.Response.Write(WrongIdentityNumber); 
      return; 
     } 
     base.OnActionExecuting(filterContext); 
    } 
} 

내 멤버 클래스는 여기에 내가 identitynumber이 사실 여부를 확인하려면 너무

public class Member 
{ 
    public int ID { get; set; } 
    [Required(ErrorMessage = "You have to enter your name")] 
    [StringLength(50, ErrorMessage ="Your name length can not be less than{2} more than {1} ", MinimumLength = 3)] 
    [Display("Name :")] 
    public string Name { get; set; } 

    [Required(ErrorMessage = "You have to enter your surname")] 
    [StringLength(40, ErrorMessage = "Your surname length can not be less than{2} more than {1} ", MinimumLength = 2)] 
    [Display("Surname :")] 
    public string SurName { get; set; } 

    [Required(ErrorMessage = "You have to enter your password")] 
    [StringLength(20, ErrorMessage = "Your name lengt can not be less than{2} more than {1} ", MinimumLength = 6)] 
    [DataType(DataType.Password)] 
    [Display("Password :")] 
    public string Password { get; set; } 
    public string PasswordSalt { get; set; } 

    [IdentityNumberControl(WrongIdentityNumber = "It is not a valid identity number")] 
    public double IdentityNumber { get; set; } 

} 

입니다 내가 IdentityNumberControl 클래스에 코드를 작성해야한다는 것을 알고 을 (나는 그것을 확인하기위한 계산 방법을 가지고) . 하지만 OnActionExecuting 메서드에서 identityNumber의 값을 가져 오는 방법을 알지 못합니다. 나는 희망

내가

여기
+1

알았어. 해결책을 찾았습니다. ActionFilterAttribute 대신 ValidationAttribute를 구현했습니다. :) – unbalanced

+0

솔루션을 찾았 으면 해답으로 받아들이십시오. 그런 식으로 미래의 사용자는 문제를 해결하는 방법을 이해합니다. – rae1

답변

0

내 솔루션 내 문제를 설명

public class IdentityNumberAttribute : ValidationAttribute 
{ 
    private string WrongIdentityNumber; 

    public IdentityNumberAttribute(string message) 
    { 
     WrongIdentityNumber = message; 
    } 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 

     string identityNumber = value.ToString(); 

     if (identityNumber.Length != 11) 
      return new ValidationResult(WrongIdentityNumber); 

     int sum = 0; 


     for (int i = 0; i < identityNumber.Length - 1; i++) 
     { 
      sum += Convert.ToInt32(identityNumber[i].ToString()); 
     } 

     return sum.ToString()[1] == identityNumber[10] 
        ? ValidationResult.Success 
        : new ValidationResult(WrongIdentityNumber); 
    } 
} 

는 계산이 유효 터키어 natioanal 식별 번호 또는하지

당신은 그

처럼이 attirbute를 사용할 수 있는지
[IdentityNumber("It is not a valid identity number")] 
    [Required(ErrorMessage = "You have to enter your identity number")] 
    [DisplayName("National Identity Number:")] 
    public string IdentityNumber { get; set; }