简体   繁体   English

在MVC中检查电子邮件的远程验证

[英]Remote Validation For Checking Email in MVC

I am using remote validation for checking if email already exist in database but it is not working, here is the code: 我正在使用远程验证来检查数据库中是否已存在电子邮件,但它不起作用,这是代码:

Model Property 模型属性

[Remote("IsAlreadyUserExist", "User", HttpMethod = "POST", ErrorMessage = "Email Already Exists.")]
public string Email { get; set; }

Controller 调节器

[HttpPost]
public JsonResult IsAlreadyUserExist(string Email)
{

    return Json(IsUserExist(Email));

}

public bool IsUserExist(string Email)
{      
    List<UserProfile> userlist = userRepo.GetUserList();
    var UserEmailId = (from u in userlist
                      where u.Email.ToUpper() == Email.ToUpper()
                      select new { Email }).FirstOrDefault();

    bool status;
    if (UserEmailId != null)
    {
        //Already registered  
        status = false;
    }
    else
    {
        //Available to use  
        status = true;
    }
    return status;
}

I have added this js in my Create view at the bottom. 我在底部的“创建”视图中添加了此js。

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

I also tried putting debugger in IsAlreadyUserExist function but it is not hitting there. 我还尝试将调试器放入IsAlreadyUserExist函数中,但没有成功。

Any idea what could be the problem? 知道可能是什么问题吗?

For making RemoteAttibute working, write your RemoteAttibute on Email property as follows: 为使RemoteAttibute正常工作,请按如下所示在“ Email属性上编写RemoteAttibute

[Required]
[StringLength(30)]
[Index("Ix_Email",Order =1,IsUnique =true)]
[Remote("IsUserAlreadyExists", "User", AdditionalFields = "Id", ErrorMessage = "User already exists with the provided email")]
public string Email { get; set; }

You are pulling all the users from the database in memory just for checking whether a user already exists with a certain email, which is very very bad considering the performance standpoint. 您正在从内存中的数据库中拉出所有用户,只是为了检查某个电子邮件是否已经存在一个用户,从性能的角度来看,这是非常糟糕的。 So write IsUserAlreadyExists method in the UserController as follows: 因此,在UserController编写IsUserAlreadyExists方法,如下所示:

public JsonResult IsUserAlreadyExists(string Email, int? Id)
{
     var isUserAlreadyExists = db.Users.Any(x => x.Email== Email && x.Id != Id);
     return Json(!isUserAlreadyExists, JsonRequestBehavior.AllowGet);
}

Now if you want to move the database access code to your UserRepository then do it as follows: 现在,如果要将数据库访问代码移至UserRepository请按以下步骤进行操作:

public class UserRepository
{
   YourDbContext dbContext = new YourDbContext();

   public bool IsUserAlreadyExistsByEmail(string email, int? id)
   {
      return dbContext.Users.Any(x => x.Email== Email && x.Id != Id);
   }
}

Then call the IsUserAlreadyExistsByEmail method of the UserRepository as follows: 然后调用IsUserAlreadyExistsByEmail的方法UserRepository如下:

public JsonResult IsUserAlreadyExists(string Email, int? Id)
{
     var isUserAlreadyExists = userRepo.IsUserAlreadyExistsByEmail(Email,Id);
     return Json(!isUserAlreadyExists, JsonRequestBehavior.AllowGet);
}

Note: I didn't see your repository code so I have shown here a generalized view of how it should be. 注意:我没有看到您的存储库代码,因此在这里显示了它应该是如何的通用视图。 Use everything according to your own structure. 根据自己的结构使用一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM