我想使用模型中的[Remote]数据注释验证数据库中是否存在Email Id。 但是当我调用远程JsonResult Action方法时,参数将为null,结果将始终为false,并将显示错误消息。我的代码有什么问题?
型号:
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;
}
}
}
帐户控制器
public JsonResult IsEmailIdExists(string EmailId) -> Always Null
{
Model.EmailExists emailCheck = new FresherModel.EmailExists();
if(!emailCheck.EmailCheck(EmailId))
{
return Json(false);
}
else
{
return Json(true);
}
}
答案 0 :(得分:1)
您的媒体资源名称为Email
。更改操作方法的签名以匹配
public JsonResult IsEmailIdExists(string Email)
{
.....