public ActionResult Register(RegisterModel r) { if (ModelState.IsValid) { if (verifyUser(r.userName)) ModelState.AddModelError("userName", "User name have been existed"); else { Profile p = new Profile { name=r.fullName, address=r.address, telephone=r.telephone, dateOfBirth=r.dateOfBirth }; db.Profiles.Add(p); db.SaveChanges(); int lastID = db.Profiles.Max(pro => pro.profileId); Account a = new Account { userName = r.userName, userPassword =PasswordHandler.Encrypt(r.passWord), profileId = lastID, roleId = 2 }; db.Accounts.Add(a); db.SaveChanges(); return RedirectToAction("Index"); } } ViewBag.roles = new SelectList(db.Roles, "roleId", "roleName"); return View(r); }
public ActionResult Edit(RegisterModel rm) { if (ModelState.IsValid) { Account ac = db.Accounts.SingleOrDefault(a => a.accountId == rm.accountId); if(verifyUser(rm.userName,true,ac.userName)){ ModelState.AddModelError("userName", "This UserName have been existed"); } else { Account account = new Account { accountId=rm.accountId, userName=rm.userName, userPassword=rm.passWord, profileId=ac.profileId, roleId=2 }; db.Entry(ac).CurrentValues.SetValues(account); db.SaveChanges(); Profile profile = new Profile { profileId =(int) ac.profileId, name = rm.fullName, address = rm.address, email=rm.email, telephone = rm.telephone, dateOfBirth = rm.dateOfBirth }; db.Entry(profile).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } } ViewBag.roles = new SelectList(db.Roles, "roleId", "roleName",rm.roleId); return View(rm); }
public ActionResult Edit(int? accountId=0) { if (accountId == 0) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var userDetail = db.Accounts.SingleOrDefault(a => a.accountId == accountId); if (userDetail == null) { return HttpNotFound(); } RegisterModel rm = new RegisterModel { accountId = userDetail.accountId, userName = userDetail.userName, passWord = userDetail.userPassword, confirmPassWord=userDetail.userPassword, roleId = (int)userDetail.roleId, fullName = userDetail.Profile.name, address = userDetail.Profile.address, email = userDetail.Profile.email, telephone = userDetail.Profile.telephone, dateOfBirth = (DateTime)userDetail.Profile.dateOfBirth }; ViewBag.roles = new SelectList(db.Roles, "roleId", "roleName",userDetail.roleId); return View(rm); }