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); }