如何正确处理控制器提交方法中的远程属性验证

时间:2014-09-05 06:16:47

标签: javascript jquery asp.net-mvc-4 asp.net-ajax unobtrusive-validation

这是我的模特

  [Required(ErrorMessage="Current password is required")]
    [Remote("IsCorrectOldPassword", "Account", ErrorMessage = "The password you gave is incorrect.")]
    [DataType(DataType.Password)]
    [Display(Name = "Current Password")]
    public string OldPassword { get; set; }

    [Required]
    [StringLength(20, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "New Password")]
    public string NewPassword { get; set; }
  

这是我的观点

 @using (Ajax.BeginForm("ChangePassword", "Account", new AjaxOptions { HttpMethod = "POST", OnSuccess = "AfterSave()", UpdateTargetId = "formcontents" }, new { @id = "formSavePwd" }))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)

@ @ Html.HiddenFor(m => m.UserId) @                     

                <table style="width: 100%">

                    @*<caption>Change Password Form</caption>*@

                    <tr>
                        <td>@Html.LabelFor(m => m.OldPassword)


                        </td>
                        <td>
                            @Html.PasswordFor(m => m.OldPassword, new { @enabled = true, maxlength = 20, size = 20 })
                            @Html.ValidationMessageFor(m => m.OldPassword)
                        </td>
                    </tr>

                    <tr>
                        <td>@Html.LabelFor(m => m.NewPassword)

                        </td>
                        <td>
                            @Html.PasswordFor(m => m.NewPassword)
                           @* @Html.ValidationMessageFor(m => m.NewPassword)*@

                        </td>
                    </tr>
  

这是我的控制器

 public ActionResult ChangePassword(LocalPasswordModel passwordModel)
     {

         if (ModelState.IsValid)
         {
             var user = new FcpUserDto();
             user.ClientId = (Int64) SessionObjects.ClientId;
             user.ClientKey = Guid.NewGuid().ToString();
             user.LoginPassword = passwordModel.NewPassword;
             user.DateModified = DateTime.Now;
             user.UserId = (long) passwordModel.UserId;
             StatusDto statusDto = _FcpEndpoint.ChangePassword(user);
             //return Json(true, JsonRequestBehavior.AllowGet);
         }
         else
         {
             var model = new LocalPasswordModel();



             var languageItems = (from ResourceLabels.UserManagementlabels resourceLabel in Enum.GetValues(typeof(ResourceLabels.UserManagementlabels))
                                  select new FcpLanguageItemsDto
                                  {
                                      ClientId = SessionObjects.ClientId,
                                      LanguageId = SessionObjects.ClientLanguageId,
                                      ObjectId = (long)resourceLabel,
                                      ObjectType = (short)EnumObjectType.SRM_SCREEN_LABELS,
                                      TokenId = (long)EnumPhrase.Label
                                  }).ToList();
             model.ResourceLabelModel = WebMapper.CreateResourceLabelModelList(_FcpEndpoint.GetPhrases(languageItems));
             ModelState.AddModelError("", "");
             return PartialView("_ChangePasswordPartial",model);
             //return Json(false, JsonRequestBehavior.AllowGet);
         }



        return null;
     }
  提交控制器方法上的

但是远程属性验证不起作用,我不知道我错过了什么,我的远程属性方法没有被调用。   下面是我对IsCorrectOldPassword的行动方法

 public JsonResult IsCorrectOldPassword(string OldPassword)
     {
         const string errorHTML = "<span class='Error'><div class='Validation-Error-Text errorImg'   > </div><div class='MSGReg'>{0}</div></span>";
         const string IncorrectOldPwd = "The password you gave is incorrect.";
         //if (FcpLocalPasswordDto.IsCorrectOldPassword(new FcpLocalPasswordDto()
         //{
         //    UserId = SessionObjects.UserId,
         //    OldPassword = OldPassword
         //}).IsSuccessful)

         var userPassword = new FcpLocalPasswordDto();
         userPassword.OldPassword = OldPassword;
         userPassword.UserId = SessionObjects.UserId;

        StatusDto statusDto = _FcpEndpoint.IsCorrectOldPassword(userPassword);
        //if successful
        if (statusDto.IsSuccessful) 
        {
            return Json(string.Format(errorHTML, IncorrectOldPwd), JsonRequestBehavior.AllowGet); 
        }

        return Json(true, JsonRequestBehavior.AllowGet);
     }   

1 个答案:

答案 0 :(得分:0)

只需要添加jquery.validate.js以上代码即可开始正常工作