How to use Remote Validation correctly?

0

I'm trying to use Remote Validation to check if the email is already registered, the problem is that whenever I report an email for example [email protected] it does not return me that the email is already registered and it is registered, and if I I put [email protected] ai returns the validation that the email is already registered, very strange it.

How do I solve this problem?

I'm trying like this.

User Class

[Serializable]
public class Usuario{

    public virtual long id              { get; set; }

    [Required(ErrorMessage = "Informe o nome")]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "Campo nome deve ter de 5 a 50 caracteres")]        
    public virtual String nome          {get;set;}

    [Remote("isExistEmail", "Usuario", ErrorMessage = "Email já cadastrado!")]
    [EmailBrasil(EmailRequerido=true)]        
    public virtual String email         {get;set;}

    [Required(ErrorMessage="Informe a senha")]
    [SenhaBrasil(SenhaTamanhoMinimo = 8, SenhaTamanhoMaximo = 8, SenhaForteRequerida = false, CaracterEspecialRequerido = false)]
    public virtual String senha         {get;set;}

    public virtual int status           {get; set;} //1 ativo, 2 inativo, 0 aguardando

    public Usuario() { }

 }

Method that verifies that the email is registered in the user's controller.

public JsonResult isExistEmail(String email){
     Boolean isExist = dao.isExist(email);            
     if (isExist){                
         return Json(true, JsonRequestBehavior.AllowGet);
     }else{
         return Json(false, JsonRequestBehavior.AllowGet);
     }
}

DAO that checks for email in database

//verifica se ja existe o email cadastrado
public Boolean isExist(String email){
    ISession _session = getSession();
    IList<Usuario> list = _session.CreateQuery("FROM Usuario u WHERE u.email = :email")
       .SetParameter("email", email)            
        .SetMaxResults(1)
        .List<Usuario>();
    if (list.Count > 0){
        return true;
    }
    return false;
}

HTML

<div class="form-group">
     <label for="email" class="cols-sm-2 control-label">Email <img src="~/Imagens/required.png" height="6" width="6"></label>
      <div class="cols-sm-10">
          <div class="input-group">
             <span class="input-group-addon"><i class="glyphicon glyphicon-envelope" aria-hidden="true"></i></span>
                 @Html.TextBoxFor(model => Model.email, new { Class = "form-control", placeholder = "Informe o email", maxlength = 255 })
          </div>
                 @Html.ValidationMessageFor(model => model.email)
      </div>
</div>
    
asked by anonymous 16.10.2016 / 18:02

1 answer

1

Resolved, so I read in the documentation when there is an occurrence, in the case of email, the return in JSON should be false and not true as I was trying, I made the change and it worked 100%.

I did so.

public JsonResult isExistEmail(String email){
            Boolean isExist = dao.isExist(email);            
            if (isExist){                
                return Json(false, JsonRequestBehavior.AllowGet);
            }else{
                return Json(true, JsonRequestBehavior.AllowGet);
            }
        }
    
17.10.2016 / 02:37