2012-03-24 2 views
0

확실하지 예상대로 작동하지 그러나 그것은 나에게 놀람이었다 ..MVC 검증이 예상 될 경우

내가 강하게 RegistrationViewModel와보기를 입력하고,하지만 난 양식을 제출, 검증을 시도 할 때 2 곳에서 잘못 검증합니다.

  1. "테스트"를 입력하면 이메일 입력란에 유효성 검사 오류가 표시되지 않습니다.
  2. 암호 유효성 검사기는 암호가있을 때 암호가 일치하지 않는다고 말합니다.

왜 이런 일이 일어나는 지 알고 계십니까?

뷰 모델 클래스 :

public class RegistrationViewModel 
{ 
    public RegisterModel RegistrationData { get; set; } 
    ...  
} 

검증 속성이 RegistrationData 클래스의 속성에 있습니다

public class RegisterModel 
{ 
    [Required] 
    [Display(Name = "User name")] 
    public string UserName { get; set; } 

    [Required] 
    [DataType(DataType.EmailAddress)] 
    [Display(Name = "Email address")] 
    public string Email { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 

    [DataType(DataType.Password)] 
    [Display(Name = "Confirm password")] 
    [Compare("Password", ErrorMessage = "The Password and Confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 

    [Required] 
    [Display(Name = "First name")] 
    [StringLength(20, ErrorMessage = "First name must be between 2 and 20 characters.", MinimumLength = 2)] 
    public string FirstName { get; set; } 

    [Required] 
    [Display(Name = "Last name")] 
    [StringLength(20, ErrorMessage = "Last name must be between 2 and 20 characters.", MinimumLength = 2)] 
    public string LastName { get; set; } 
} 

보기 :

@model PropertyManager_MVC.Areas.Account.ViewModels.RegistrationViewModel 
<div class="editor-field"> 
      @Html.TextBoxFor(m => m.RegistrationData.Email) 
      @Html.ValidationMessageFor(m => m.RegistrationData.Email) 
     </div> 
<div class="editor-label"> 
      @Html.LabelFor(m => m.RegistrationData.Password) 
     </div> 
     <div class="editor-field"> 
      @Html.PasswordFor(m => m.RegistrationData.Password) 
      @Html.ValidationMessageFor(m => m.RegistrationData.Password) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(m => m.RegistrationData.ConfirmPassword) 
     </div> 
     <div class="editor-field"> 
      @Html.PasswordFor(m => m.RegistrationData.ConfirmPassword) 
      @Html.ValidationMessageFor(m => m.RegistrationData.ConfirmPassword) 
     </div> 
+0

서버 측 유효성 확인이란 무엇입니까? – Jon

+0

아니요, 클라이언트 측 유효성 검사 – Greg

+0

webconfig에서 클라이언트 측 유효성 검사와 눈에 잘 띄지 않는 j를 모두 사용할 수 있습니다. – Greg

답변

2

부동산의 이메일이 정규 표현식을 추가하고 그것을 처리 할 것입니다.

[Required] 
    [DataType(DataType.EmailAddress)] 
    [Display(Name = "Email address")] 
    [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9]+(\\.[a-z]{2,4})$", ErrorMessage = "Not a valid email")] 
    public string Email { get; set; } 

암호 확인 필드 앞에 필수 속성을 추가하십시오.

[Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "Confirm password")] 
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 

이 문제를 해결하기위한 Scottgu의 예. http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

+0

고마워요 - 그럼 DataType.Email은 무엇을합니까? – Greg

+1

@Greg - DataType 속성은 유효성 검사에 영향을주지 않고 표시 및 편집기 템플리트에 영향을줍니다. DataType.Password는 DisplayTemplate이 Html.DisplayFor를 사용할 때 mailto : 링크로 암호의 서식을 지정합니다. 원할 경우이 설정을 덮어 쓰고 자신 만의 이메일 템플릿을 만들 수도 있습니다. –

+0

처음 테스트했을 때 작동하고 있었지만 유효성 검사 오류가 다시 나타났습니다 (변경 사항이 아직 적용 되었음에도 불구하고). 미안 Shyju, 답변을 다시 표시했습니다. 나는 이것을위한 해결책을 찾아야한다. – Greg

관련 문제