2012-11-20 11 views
5

C'è qualche problema nell'uso di @Html.ValidationSummary() all'interno di un modulo Ajax.BeginForm?@ Html.ValidationSummary() non funziona all'interno di Ajax.BeginForm

Ho il seguente scenario e non posso ottenere la convalida per i campi obbligatori. Il modulo è appena pubblicato e non viene generato alcun errore.

Questa è la vista:

@using (Ajax.BeginForm("Register", "Account", new AjaxOptions { HttpMethod = "POST", OnSuccess = "closeDialog('RegistroUsuario')" })) 
{ 
    @Html.ValidationSummary() 
    <fieldset> 
     <legend>Cadastro novo Usuário</legend> 
     <table id="changePassword"> 
       <tr> 
        <td class="smallField">Username:</td> 
        <td>@Html.TextBoxFor(m => m.UserName)</td> 
       </tr> 
       <tr> 
        <td>Password:</td> 
        <td>@Html.PasswordFor(m => m.Password)</td> 
       </tr> 
       <tr> 
        <td>Repetir Senha:</td> 
        <td>@Html.PasswordFor(m => m.ConfirmPassword)</td> 
       </tr> 
       <tr> 
        <td>Email:</td> 
        <td>@Html.TextBoxFor(m => m.Email)</td> 
       </tr> 
       <tr> 
        <td>Pergunta Secreta:</td> 
        <td>@Html.TextBoxFor(m => m.SecretQuestion)</td> 
       </tr> 
           <tr> 
        <td>Resposta:</td> 
        <td>@Html.TextBoxFor(m => m.SecretQuestionPassword)</td> 
       </tr> 
       <tr> 
        <td>Ativo:</td> 
        <td><input type="checkbox" name="status" id="status" value="Ativo"></td> 
       </tr>  
      </table>   
    </fieldset> 
    <input type="submit" value="Criar Usuário" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only button-link"/> 
} 

thats il controller:

// 
    // POST: /Account/Register 
    [HttpPost] 
    public ActionResult Register(RegisterModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      MembershipProvider mp = Membership.Provider; 
      MembershipCreateStatus Status;     

      // Tenta registrar o usuário 
      try 
      { 
       //Verifica se usuário deve estar ativo ou não no sistema 
       if (String.IsNullOrEmpty(Request.Form["status"])) 
       { 
        model.Active = false; 
       } 
       else 
       { 
        model.Active = true; 
       } 

       //Cria o usuário 
       MembershipUser newUser = mp.CreateUser(model.UserName, model.Password, model.Email, model.SecretQuestion, model.SecretQuestionPassword, model.Active, Guid.NewGuid(), out Status); 

       if (newUser == null) 
       { 
        /**/ 
       } 
       else 
       {      
        return RedirectToAction("Index", "Home"); 
       } 

      } 
      catch (MembershipCreateUserException e) 
      { 
       ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

e il modello:

public class RegisterModel 
{ 
    [Required] 
    [Display(Name = "Usuário")] 
    public string UserName { get; set; } 

    [Required] 
    [Display(Name = "Email")] 
    public string Email { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage = "A {0} deve ter no mínimo {2} caracteres.", MinimumLength = 6)] 
    [DataType(DataType.Password)] 
    [Display(Name = "Senha")] 
    public string Password { get; set; } 

    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "Repetir Senha")] 
    [Compare("Password", ErrorMessage = "As senhas não coincidem")] 
    public string ConfirmPassword { get; set; } 

    [Required] 
    [Display(Name = "Pergunta Secreta")] 
    public string SecretQuestion { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage = "A {0} deve ter no mínimo {2} caracteres.", MinimumLength = 6)] 
    [DataType(DataType.Password)] 
    [Display(Name = "Senha Pergunta Secreta")] 
    public string SecretQuestionPassword { get; set; } 

    [Required] 
    [Display(Name = "Ativo")] 
    public bool Active { get; set; } 
} 

Mi sto perdendo somenthing?

+0

Bene, cosa dice il debugger? Sei arrivato al 'return View (model)' a tutti? Il sommario sembra OK dov'è. –

risposta

7

Trovato il problema.

L'ID destinazione Ajax.Options non è stato impostato. Una volta impostato l'elemento dom di convalida ora ricevo i messaggi.

Grazie mille

+0

Che cos'è "validation dom"? Quando ho giocato con il tuo esempio, devo impostare UpdateTargetId sull'elemento radice della vista, che tu restituisci da Controller e lo renderà interamente rasterizzato –

+0

@Philipp, questo è un vecchio progetto. A quel tempo stavo lavorando a quel progetto che non avevo esperienza su quello che stavo facendo e ho trovato una soluzione che non sono sicuro fosse la migliore. Aprirò il progetto alla sera e tornerò da te con la chioccia. –

2

di essere specifico su Guilherme Longo risposta

Set AjaxOptions come

new AjaxOptions() { HttpMethod = "Post", OnSuccess = "RefreshMethod", UpdateTargetId = "FormId" } 

Avviso il UpdateTargetId impostati formid. Ha funzionato per me Grazie Guilherme

Problemi correlati