2014-02-20 47 views
3

我有一個類在我的模型中MVC:比較電子郵件地址輸入到數據庫DataAnnotations

public class NewModel 
{ 
    public bool AllComplexes { get; set; } 
    public int UserID { get; set; } 
    public int? RoleID { get; set; } 
    public int ComplexID { get; set; } 

    [Required(ErrorMessage = "Please enter a user name."), StringLength(50)] 
    public string Username { get; set; } 

    [Required(ErrorMessage = "Please enter Password"), StringLength(100, ErrorMessage = "Password cannot be longer than 100 characters")] 
    public string Password { get; set; } 

    [Compare("Password", ErrorMessage = "Passwords do not match")] 
    [Required(ErrorMessage = "Please confirm Password")] 
    public string RetypePassword { get; set; } 

    [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9]+)*\\.([a-z]{2,4})$" , ErrorMessage = "Invalid email format.")] 
    [Required(ErrorMessage = "Please enter your e-mail address."), StringLength(50)] 
    public string Email { get; set; } 

    public List<NEWCategoryModel> Categories { get; set; } 
    //public List<NEWPrivilegeModel> userPrivList { get; set; } 
    public List<DropDownItem> ComplexList { get; set; } 
    public List<DropDownItem> RoleList { get; set; } 
    public string NewRole { get; set; } 

    public NewModel() 
    { 

    } 
} 

輸入的電子郵件地址存儲在:

public string Email { get; set; } 

我需要比較的電子郵件地址使用數據註釋存儲在數據庫中的所有電子郵件地址。我假設我需要一個自定義數據註釋?但我不知道該怎麼做。

這是查詢的這個例子中的人從數據庫的電子郵件地址:

db.ContactInformations.Where(x => x.EMail != null).Select(x => x.EMail); 

回答

5
public class NewModel 
{ 
     [EmailValidation(ErrorMessage = "The Email Address already exists")] 
     [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9]+)*\\.([a-z]{2,4})$" , ErrorMessage = "Invalid email format.")] 
     [Required(ErrorMessage = "Please enter your e-mail address."), StringLength(50)] 
     public string Email { get; set; } 
{ 


public class EmailValidation : ValidationAttribute 
{ 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
     PropmetEntities db = new PropmetEntities(); 
     if (value != null) 
     { 
      var valueAsString = value.ToString(); 
      IEnumerable<string> email = db.ContactInformations.Where(x => x.EMail != null).Select(x => x.EMail); 
      if (email.Contains(valueAsString)) 
      { 
       var errorMessage = FormatErrorMessage(validationContext.DisplayName); 
       return new ValidationResult(errorMessage); 
      } 
     } 
     return ValidationResult.Success; 
    } 
} 
1

This可幫助您創建自定義的驗證。然後,檢查用戶已經在數據庫中存在的郵件,請嘗試:

bool exist = db.UserTable.Any(e => e.Email.ToLower() == emailValue.ToLower()); 
+0

我編輯我的答案,使用環節,添加存在邏輯到IsValid的方法存在。 –

1

this post,你會找到一個解決方案充分利用FluentValidation,它實現了一個自定義的DataAnnotation。

你唯一的電子郵件驗證看起來會沿着這些路線:

[Validator(typeof(NewModelValidator))] 
class NewModel 
{ 
    //...Model implementation omitted 
} 

public class NewModelValidator : AbstractValidator<NewModel> 
{ 
    public NewModelValidator() 
    { 

     RuleFor(x => x.Email).Must(IsUnieuqEmail).WithMessage("Email already exists"); 
    } 

    private bool IsUniqueEmail(string mail) 
    { 
     var _db = new DataContext(); 
     if (_db.NewModel.SingleOrDefault(x => x.Email == mail) == null) return true; 
     return false; 
    } 
} 
相關問題