public ActionResult Atualizar(UserModel user) { if (ModelState.IsValid) { if (user.SelectedRole == null) { ViewBag.Skills = db.Skills.ToList(); var roles = db.Roles.Where(r => r.RoleName != "Administrador").ToList(); ModelState.AddModelError("selectedRole", "O campo tipo é obrigatório."); ViewBag.SelectedRole = CreateSelectList<RoleModel>(roles, v => v.ID, t => t.RoleName); return View("Cadastrar", user); } user.Role = db.Roles.Where(r => r.ID == user.SelectedRole).FirstOrDefault(); db.Users.Attach(user); var entry = db.Entry(user); entry.Property(p => p.Name).IsModified = !string.IsNullOrEmpty(user.Name); entry.Property(p => p.Email).IsModified = !string.IsNullOrEmpty(user.Email); entry.Property(p => p.Username).IsModified = !string.IsNullOrEmpty(user.Username); entry.Property(p => p.Password).IsModified = !string.IsNullOrEmpty(user.Password); try { db.SaveChanges(); TempData.Add("success", "Usuário atualizado com sucesso!"); return RedirectToAction("Index"); } catch (DataException) { var userRole = Roles.GetRolesForUser().FirstOrDefault(); List<RoleModel> roles = new List<RoleModel>(); if (user.RoleName != "Administrador") roles = db.Roles.Where(r => r.RoleName != "Administrador").ToList(); else roles = db.Roles.ToList(); ViewBag.Skills = db.Skills.ToList(); ViewBag.SelectedRole = CreateSelectList<RoleModel>(roles, v => v.ID, t => t.RoleName); ModelState.AddModelError(string.Empty, "Não foi possível salvar as alterações. Tente novamente, se o problema persistir, procure o Administrador."); return View("Cadastrar", user); } } else { if (user.SelectedRole == null) ModelState.AddModelError("selectedRole", "O campo tipo é obrigatório."); List<RoleModel> roles = new List<RoleModel>(); if (user.RoleName != "Administrador") roles = db.Roles.Where(r => r.RoleName != "Administrador").ToList(); else roles = db.Roles.ToList(); ViewBag.SelectedRole = CreateSelectList<RoleModel>(roles, v => v.ID, t => t.RoleName); ViewBag.Skills = db.Skills.ToList(); } return View("Cadastrar", user); }
public ActionResult Cadastrar(UserModel model) { if (ModelState.IsValid) { try { if (model.SelectedRole == null) { ViewBag.Skills = db.Skills.ToList(); var roles = db.Roles.Where(r => r.RoleName != "Administrador").ToList(); ModelState.AddModelError("selectedRole", "O campo tipo é obrigatório."); ViewBag.SelectedRole = CreateSelectList<RoleModel>(roles, v => v.ID, t => t.RoleName); return View("Cadastrar", model); } if (model.SelectedSkills != null && model.SelectedSkills.Count() > 0) { model.Skills = new List<SkillModel>(); foreach (var skill in model.SelectedSkills) { model.Skills.Add(db.Skills.Find(skill)); } } model.Role = db.Roles.Where(r => r.ID == model.SelectedRole).FirstOrDefault(); db.Users.Add(model); db.SaveChanges(); TempData.Add("success", "Usuário criado com sucesso!"); return RedirectToAction("Index"); } catch (Exception) { var userRole = Roles.GetRolesForUser().FirstOrDefault(); List<RoleModel> roles = new List<RoleModel>(); roles = db.Roles.Where(r => r.RoleName != "Administrador").ToList(); ViewBag.Skills = db.Skills.ToList(); ViewBag.SelectedRole = CreateSelectList<RoleModel>(roles, v => v.ID, t => t.RoleName); ModelState.AddModelError(string.Empty, "Não foi possível salvar as alterações. Tente novamente, se o problema persistir, procure o Administrador."); return View(model); } } else { List<RoleModel> roles = new List<RoleModel>(); if(model.SelectedRole == null) ModelState.AddModelError("SelectedRole", "O campo tipo é obrigatório."); roles = db.Roles.Where(r => r.RoleName != "Administrador").ToList(); ViewBag.Skills = db.Skills.ToList(); ViewBag.SelectedRole = CreateSelectList<RoleModel>(roles, v => v.ID, t => t.RoleName); } return View(model); }