0
這是我的模型如何處理遠程屬性驗證在控制器提交方法適當
[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);
}
安置自己的行動方法'IsCorrectOldPassword' – 2014-09-05 06:30:17
我已經加入我的IsCorrectOldPassword方法在後 – tspga 2014-09-05 06:37:02
注意確定你的一些方法在這裏做什麼,但從你命名的方式'if(statusDto.IsSuccessful)'應該返回'true'而不是錯誤消息?你有沒有在該方法中加入一個斷點並逐步完成代碼? – 2014-09-05 06:49:55