2015-05-06 19 views
5

Quando ho inserito il valore per la posta elettronica nella casella di testo, non è stato attivato il metodo VerifyUserEmail. Di seguito è il mio codice. Dov'è il mio errore?La convalida remota non funziona

View

<div class="form-group"> 
    @Html.LabelFor(model => model.User_Email, new { @class = "control-label col-lg-2" }) 
     <div class="col-lg-4"> 
      @Html.EditorFor(model => model.User_Email) 
      @Html.ValidationMessageFor(model => model.User_Email) 
     </div> 
</div> 

Modello

[DisplayName("Email")]  
[Remote("VerifyUserEmail", "User", ErrorMessage = "Email already exists!")] 
public string User_Email { get; set; } 

controller

public JsonResult VerifyUserEmail(string User_Email) 
    { 
     try 
     { 
      using (RKDYMEntities objConnection = new RKDYMEntities()) 
      { 
       ObjectParameter objIErrorCode = new ObjectParameter("ErrorCode", typeof(Int32)); 
       ObjectParameter objBFlag = new ObjectParameter("bFlg", typeof(bool)); 
       objConnection.Check_User_Exists(User_Email, objBFlag, objIErrorCode); 

       if (Convert.ToBoolean(objBFlag.Value) != true) 
       { 
        return Json(false, JsonRequestBehavior.AllowGet); 
       } 
       else 
       { 
        return Json(true, JsonRequestBehavior.AllowGet); 
       } 


      } 
     } 
     catch (Exception Ex) 
     { 
      return Json(false, JsonRequestBehavior.AllowGet); 
     } 
    } 

Ho anche aggiunto <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> in web.config.

codice html indicato qui viene generato:

<div class="form-group"> 
    <label class="control-label col-lg-2" for="User_Email">Email</label> 
     <div class="col-lg-4"> 
      <input class="text-box single-line" data-val="true" data-val-length="The field Email must be a string with a maximum length of 200." data-val-length-max="200" data-val-remote="Email already exists!" data-val-remote-additionalfields="*.User_Email" data-val-remote-url="/User/VerifyUserEmail" id="User_Email" name="User_Email" type="text" value="" /> 
      <span class="field-validation-valid" data-valmsg-for="User_Email" data-valmsg-replace="true"></span> 
     </div> 
</div> 

Jquery convalida

sto utilizzando lato client convalida JQuery che sta lavorando bene.

$(document).ready(function() { 
    $("#Registration").validate({ 
     rules: { 
      User_Email: { 
       required: true, email: true 
      }, 
      User_Password: { 
       required: true, 
       minlength: 8 
      }, 
      User_ReTypePassword: { 
       required: true, 
       minlength: 8, 
       equalTo: '#User_Password' 
      }, 
      User_FirstName: { required: true }, 
      User_LastName: { required: true }, 
      User_ZipCode: { 
       required: true, 
       digits: true 
      } 
     }, 
     messages: { 
      User_Email: { 
       required: 'Email is required', email: 'Invalid email' 
      }, 
      User_Password: { 
       required: 'Password is required', 
       minlength: 'Min length of password is 8' 
      }, 
      User_ReTypePassword: 
       { 
        required: 'Confirm Password is required', 
        equalTo: 'Confirm Password must be equal to password' 
       }, 
      User_ZipCode: { required: 'ZipCode is required', digits: 'Only digits are allowed' }, 
      User_FirstName: { required: 'First Name is required' }, 
      User_LastName: { required: 'Last Name is required' } 
     }, 
     errorPlacement: function (error, element) { 
      error.insertAfter(element); 
     }, 
     showErrors: function (errorMap, errorList) { 
      this.defaultShowErrors(); 
     } 
    }); 
}); 

Modifica

Ho rimosso

$("#Registration").validate({ // rules: { e che sta funzionando.

Qualche idea? Perché?

+0

Prova ad aggiungere '[AllowAnonymous]' al metodo 'VerifyUserEmail' –

+0

State usando compreso jQuery, jQuery. validare, jquery.validate.unobtrusive? – Mackan

+0

@ Mackan.Sì, tutti sono stati caricati correttamente. –

risposta

2

Il messaggio di errore dall'attributo dati non viene mai utilizzato (È possibile verificare questo utilizzando il mio semplice esempio qui sotto):

[Remote("VerifyUserEmail", "User"] 

accetta a distanza di un sovraccarico, con un messaggio di errore, ma non fa nulla con esso, si dovrebbe restituire il messaggio di errore dal lato server.

In pratica, qualsiasi cosa diversa da un vero ritorno è il messaggio di errore inviato al client. Se aggiungi il messaggio invece di restituire false dovresti vedere questo.

return Json(string.Format("{0} does not exist.", User_Email), 
       JsonRequestBehavior.AllowGet); 

È inoltre necessario assicurarsi di avere i seguenti riferimenti:

<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script> 
<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

semplice esempio di lavoro

Modello

public class AModel 
{ 
    [DisplayName("Email")] 
    [Remote("VerifyUserEmail", "Home")] 
    public string User_Email { get; set; } 
} 

Controller:

01.235.
public JsonResult VerifyUserEmail(string User_Email) 
    { 

      if (User_Email == "1") 
      { 
       return Json("No", JsonRequestBehavior.AllowGet); 
      } 
      else 
      { 
       return Json(true, JsonRequestBehavior.AllowGet); 
      } 
    } 

View

<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script> 
<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

@using (Html.BeginForm()) 
{ 

    <div class="form-group"> 
     @Html.LabelFor(model => model.User_Email, new { @class = "control-label col-lg-2" }) 
     <div class="col-lg-4"> 
      @Html.EditorFor(model => model.User_Email) 
      @Html.ValidationMessageFor(model => model.User_Email) 
     </div> 
    </div> 
    <input type="submit" /> 
} 

Colpo di schermo

screen shot 1

screen shot 2

+0

non viene nemmeno colpita - sicuramente questo non può essere la causa – Mackan

+0

Sei sicuro, come sto anche usando 'Validazioni remote' in uno dei miei progetti con l'attributo' ErrorMessage' e funziona perfettamente. –

+0

'RemoteAttribute' può richiedere un' ErrorMessage' –

Problemi correlati