//Here Is my code below I am using Remote validation
public ActionResult Settings()
{
return View();
}
//For checking New Password Doesnt match With Old Password
public JsonResult IsValidUsername(string newPassword)
{
AdminRepository service = new AdminRepository();
return Json(service.IsValidUsername(newPassword), JsonRequestBehavior.AllowGet);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Settings(AdminViewModels model, FormCollection collection)
{
try
{
ModelState.Remove("EmailID");
ModelState.Remove("IsValid");
if (ModelState.IsValid)
{
AdminViewModels adminObj = (AdminViewModels)Session["Admin"];
model.AdminID = adminObj.AdminID;
string newPassword = collection["NewPassword"];
if (!string.IsNullOrEmpty(newPassword))
{
bool exists = admin.PasswordUpdate(model, newPassword);
if (exists)
ViewBag.message = "Password Updated Successfully";
else
ViewBag.messageinvalid = "Invalid Password";
}
else
{
ViewBag.nullmessage = "Enter New Password";
}
}
return View();
}
catch (Exception ex)
{
return View("Error", new HandleErrorInfo(ex, "model", "collection"));
}
}
//这是我的存储库的代码
public bool IsValidUsername(string newpassword)
{
using (dbHealthSplashEntities dbcontext = new dbHealthSplashEntities())
{
return !dbcontext.Admins.Any(user => user.Password == newpassword);
}
}
//这是我的视图模型
public class AdminViewModels:IValidatableObject
{
public int AdminID { get; set; }
[Required(ErrorMessage = "EmailID is required")]
[StringLength(16, ErrorMessage = "Must be between 5 and 50 characters", MinimumLength = 5)]
[RegularExpression("^[a-zA-Z0-9_\\.-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$", ErrorMessage = "E-mail is not valid")]
[EmailAnnotation]
public string EmailID { get; set; }
[Required(ErrorMessage = "Password is required")]
//[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required(ErrorMessage = "Enter New Password")]
//[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "newPassword")]
[Remote("IsValidUsername", "Admin", ErrorMessage = "It Seems You Have Entered Same Password As Old Password!!!")]
public string newPassword { get; set; }
[Required]
[System.ComponentModel.DataAnnotations.Compare("newPassword", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
public Nullable<bool> Flag { get; set; }
public bool RememberMe { get; set; }
public bool IsValid { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext context)
{
if (newPassword == Password)
yield return new ValidationResult("Passwords should not be the same");
}
}
//我无法检查我的旧密码是否与新密码匹配 我想从旧密码验证它,如果它匹配密码,它应该抛出错误请帮助我从这
答案 0 :(得分:0)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Settings(AdminViewModels model, FormCollection collection)
{
try
{
ModelState.Remove("EmailID");
ModelState.Remove("IsValid");
if (ModelState.IsValid)
{
AdminViewModels adminObj = (AdminViewModels)Session["Admin"];
model.AdminID = adminObj.AdminID;
string newPassword = collection["NewPassword"];
if(model.Password== newPassword )
{
viewbag.errormessage="Old Password and new password cannot be same";
}
else
{
if (!string.IsNullOrEmpty(newPassword))
{
bool exists = admin.PasswordUpdate(model, newPassword);
if (exists)
ViewBag.message = "Password Updated Successfully";
else
ViewBag.messageinvalid = "Invalid Password";
}
else
{
ViewBag.nullmessage = "Enter New Password";
}
}
return View();
}
return View();
}
catch (Exception ex)
{
return View("Error", new HandleErrorInfo(ex, "model", "collection"));enter code here
}
}`enter code here`