public void InsertOrUpdate(DepartmentUser departmentuser)
 {
     if (departmentuser.Id == default(int)) {
         // New entity
         context.DepartmentUsers.Add(departmentuser);
     } else {
         // Existing entity
         context.Entry(departmentuser).State = EntityState.Modified;
     }
 }
 public ActionResult Create(DepartmentUser departmentuser)
 {
     if (ModelState.IsValid) {
         departmentuserRepository.InsertOrUpdate(departmentuser);
         departmentuserRepository.Save();
         return RedirectToAction("Index");
     } else {
         ViewBag.PossibleDepartment = departmentRepository.All;
         return View();
     }
 }
Exemplo n.º 3
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    MembershipCreateStatus createStatus;
                    Membership.CreateUser(model.UserName, model.Password, "*****@*****.**", null, null, true, null,
                                          out createStatus);

                    if (createStatus == MembershipCreateStatus.Success)
                    {
                        Roles.AddUserToRole(model.UserName, "User");
                        var departmentUser = new DepartmentUser
                                                 {UserName = model.UserName, DepartmentId = model.DepartmentId};
                        departmentUserRepository.InsertOrUpdate(departmentUser);
                        departmentUserRepository.Save();

                        return RedirectToAction("Index", "Account");
                    }

                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            // If we got this far, something failed, redisplay form
            PopulateDropDownList();
            return View(model);
        }