2012-11-20 25 views
5

Có sự cố nào khi sử dụng @Html.ValidationSummary() bên trong biểu mẫu Ajax.BeginForm không?@ Html.ValidationSummary() không hoạt động trong Ajax.BeginForm

Tôi có kịch bản sau và tôi không thể xác thực cho các trường bắt buộc. Biểu mẫu chỉ được đăng và không có lỗi nào được đưa ra.

Đây là Xem:

@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"/> 
} 

That's 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); 
    } 

Và Model:

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; } 
} 

Tôi có thiếu somenthing?

+0

Vâng, trình gỡ lỗi của bạn cho bạn biết điều gì? Bạn có nhận được 'return View (model)' không? Tóm tắt trông OK ở đâu. –

Trả lời

7

Đã tìm thấy sự cố.

Id mục tiêu Ajax.Options chưa được đặt. Một khi tôi đã đặt thành phần tử dom xác thực bây giờ tôi nhận được tin nhắn.

Thanks a lot

+0

"dom xác thực" là gì? Khi tôi chơi với ví dụ của bạn, tôi phải thiết lập UpdateTargetId thành phần tử gốc của khung nhìn, mà bạn quay trở lại bằng Controller và nó sẽ lập lại nó hoàn toàn –

+0

@Philipp, đây là một dự án cũ. Lúc đó tôi đang làm việc cho dự án đó mà tôi không có chuyên môn về những gì tôi đang làm và tôi đã tìm ra một giải pháp mà tôi không chắc đó là giải pháp tốt nhất. Tôi sẽ mở dự án đó tại đây và tôi sẽ quay lại với bạn với awnser. –

2

Để cụ thể trên Guilherme Longo trả lời

Set AjaxOptions như

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

Thông báo các UpdateTargetId thiết lập để formId. Làm việc cho tôi Cảm ơn Guilherme

Các vấn đề liên quan