Hi, I have a sign-up page where the user can input his FirstName, LastName, Email and Password, along with other fields.
I have bound validation attributes to this Model (called "User" and created via LINQtoSQL) and all works well.
Model code:
[MetadataType(typeof(UserValidation))]
public partial class User { }
[Bind(Exclude = "UserID")]
[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirm password don't match.")]
public class UserValidation
{
[Required(ErrorMessage = "First name required"), StringLength(20, MinimumLength=3, ErrorMessage = "Must be between 3 and 20 characters")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last name required"), StringLength(20, MinimumLength = 3, ErrorMessage = "Must be between 3 and 20 characters")]
public string LastName { get; set; }
[Required(ErrorMessage = "Email address required"), RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Must be a valid email address")]
public string Email { get; set; }
[Required(ErrorMessage = "Password required"), StringLength(20, MinimumLength = 6, ErrorMessage = "Password must be between 6 and 20 characters")]
public string Password { get; set; }
[Required(ErrorMessage = "Confirm password required"), StringLength(20, MinimumLength = 6, ErrorMessage = "Password must be between 6 and 20 characters")]
public string ConfirmPassword { get; set; }
}
After sign-up and login, I want the user to be able to edit their FirstName, LastName and Email (lets call these "Account" fields) in one View and "Password" in another. This is where my problem lies.
When I submit a form updating the Account fields data via the same Model ("User") used in the sign-up, the IsValid method flairs up a ModelState error for the missing "Password" field.
Controller code:
//
// GET /Talent/Account
public ActionResult Account()
{
string cookieUser = User.Identity.Name;
User user = userRepository.GetUserByEmail(cookieUser);
return View(user);
}
// POST /Talent/Account
[HttpPost]
public ActionResult Account(User model)
{
if (ModelState.IsValid)
{
// do something
ModelState.AddModelError("", "All good.. "+ model.FirstName + " - " + model.LastName);
}
return View(model);
}
How can I get around this?? Best practice etc...