Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

sql server - Unable to Validate Existing email Id using [Remote] data annotation attribute

I want to validate existence of Email Id in database using [Remote] data Annotation in Model. But when I call a remote JsonResult Action method the parameter will be null and the result will be always be false and error message will be displayed. What is the problem in my code?

Model :

public class RegisterModel
{
    [Required(ErrorMessage = "Email is Required!", AllowEmptyStrings = false)]
    [Remote("IsEmailIdExists", "Account", ErrorMessage = "Email Id already exists in Database")]
    [Display(Name = "Email Id")]
    [RegularExpression("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}", ErrorMessage = "Invalid Email Id")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Password is Required!", AllowEmptyStrings = false)]
    [DataType(DataType.Password)]
    [RegularExpression(@"(?=^.{8,15}$)((?=.*d)(?=.*[A-Z])(?=.*[a-z])|(?=.*d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*", ErrorMessage = "Invalid Password!")]
    public string Password { get; set; }

    [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "Confirm password dose not match!")]
    [Required(ErrorMessage = "Confirm Password is Required!")]
    [DataType(DataType.Password)]
    public string ConfirmPassword { get; set; }
}


public class EmailExists
    {
         public bool EmailCheck(string _email)
         {
              try
              {
                 var con = new SqlConnection();
                 con.ConnectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
                 SqlCommand com = new SqlCommand("spCheckEmail", con);
                 com.CommandType = CommandType.StoredProcedure;
                 com.Parameters.Add("@uname", SqlDbType.NVarChar, -1).Value = _email;
                 con.Open();
                 com.CommandTimeout = 120;
                 SqlDataReader reader = com.ExecuteReader();
                 if (reader.HasRows)
                 {
                     if (reader.Read())
                     {
                          if (reader["Active"].ToString() == "True")
                          {
                               reader.Dispose();
                               com.Dispose();
                               return true;
                          }
                          else
                          {
                               return false;
                          }
                     }
                     else
                     {
                          return false;
                     }
                 }
                 else
                 {
                     reader.Dispose();
                     com.Dispose();
                     return false;
                 }
                 con.Close();
                 com.Dispose();
            }
            catch(Exception ex)
            {
                return false;
            }
       }
   }

Account Controller

public JsonResult IsEmailIdExists(string EmailId) -> Always Null

{
      Model.EmailExists emailCheck = new FresherModel.EmailExists();
      if(!emailCheck.EmailCheck(EmailId))
      {
           return Json(false);
      }
      else
      {
          return Json(true);
      }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The name of your property is Email. Change the signature of your action method to match

public JsonResult IsEmailIdExists(string Email)
{
  .....

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...