Unable to Validate Existing email Id using [Remote] data annotation attribute(无法使用 [Remote] 数据注释属性验证现有电子邮件 ID)
问题描述
我想使用模型中的 [远程] 数据注释来验证数据库中电子邮件 ID 的存在.但是当我调用远程 JsonResult Action 方法时,参数将为空,结果将始终为 false 并显示错误消息.我的代码有什么问题?
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?
模型:
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);
}
}
推荐答案
您的资产名称是 Email
.更改您的操作方法的签名以匹配
The name of your property is Email
. Change the signature of your action method to match
public JsonResult IsEmailIdExists(string Email)
{
.....
这篇关于无法使用 [Remote] 数据注释属性验证现有电子邮件 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法使用 [Remote] 数据注释属性验证现有电子邮件 ID


- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- SQL 临时表问题 2022-01-01