I’ve got a auto-generated Create action in my controller, I’ve changed things a little, I have to modify the submitted model before I call ModelState.IsValid().
But the IsValid() is still saying its failed, I think this is because it goes off the received parameter, and ignores the changes I’ve made? How can I get it to validate after I’ve changed things?
Thanks
Controller code:
[HttpPost]
[CaptchaValidator]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register(User user, bool captchaValid)
{
user.prepareUserForCreation();
if (ModelState.IsValid && captchaValid)
{
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewData["captchaValidationMessage"] = captchaValid ? "" : "Your words didn't match, please try again";
return View(user);
}
Prepare method:
public void prepareUserForCreation()
{
this.DateCreated = DateTime.Now;
this.LastSeen = DateTime.Now; //TODO remove this
this.IsActivated = true; //TODO remove this
SecurityHelper.HashPassword(this);
}
Basically its saying password is null.
You must use
ModelState.Clear();before you use prepare method and then callTryValidateModel(user)to validate it