c# - 将输入到数据库的电子邮件地址与 DataAnnotations 进行比较

标签 c# asp.net-mvc email data-annotations modelstate

我的 MVC 模型中有一个类:

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);

最佳答案

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;
    }
}

关于c# - 将输入到数据库的电子邮件地址与 DataAnnotations 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21903355/

相关文章:

c# - 向串口发送数据时的异步数字时钟

c# - 如何在 ASP.NET 中保护将发送的电子邮件分类为垃圾邮件

c# - 如何从文本框中仅获取 sql 中的月份和年份值,其中 textmode 属性为月份? ASP.NET C#

c# - 带有 json 的 asp web.api 的最大 http 请求大小

C# 验证签名

c# - 当用户在 asp.net mvc3 中未被授权时重定向到另一个页面

c# - PDFsharp 找不到图像(找不到图像)

c# - 如何在我的数据库中生成成员(member)提供者表

javascript - 电子邮件模板的背景图像在 outlook 任何版本中均不起作用

允许空值的 Javascript 电子邮件验证