public User CreateUser() { User item = new User(); item.UserName = "******"; item.FirstName = "firstname"; item.LastName = "LastName"; item.Email = "*****@*****.**"; return item; }
public void Create_Action_Post_Should_Return_View_When_UnSuccessful() { // Arrange var controller = CreateAccountController(); controller.ModelState.AddModelError("An error", "Message"); // Act var user = new User(); var result = controller.Create(user) as ViewResult; // Assert Assert.IsInstanceOfType(result.ViewData.Model, typeof(User)); }
public virtual ActionResult Create(User user) { if (ModelState.IsValid) { if (Request.Form["RoleIds"].IsNotNull()) { UpdateUserRoles(Request.Form["RoleIds"].ToArray(), user); } _repository.InsertOrUpdate(user); this.FlashInfo("User Created"); return RedirectToAction("Index"); } this.FlashWarning("Error creating account"); ViewBag.PossibleRoles = _repository.GetRoles(); return View(user); }
private void UpdateUserRoles(IEnumerable<string> selectedRoles, User userToUpdate) { if (selectedRoles.IsNull()) { userToUpdate.Roles.Clear(); return; } var selectedRolesHS = new HashSet<string>(selectedRoles); var userRoles = new HashSet<int>(userToUpdate.Roles.Select(c => c.Id)); var allRoles = _repository.GetRoles(); foreach (var role in allRoles) { if (selectedRolesHS.Contains(role.Id.ToStringInvariantCulture())) { if (!userRoles.Contains(role.Id)) { userToUpdate.AddRole(role); } } else { if (userRoles.Contains(role.Id)) { userToUpdate.RemoveRole(role); } } } }
private void PopulateAssignedRolesData(User user) { var allRoles = _repository.GetRoles(); var userRoles = new HashSet<int>(user.Roles.Select(c => c.Id)); var viewModel = allRoles.Select(role => new AssignedRole { RoleID = role.Id, RoleName = role.RoleName, Assigned = userRoles.Contains(role.Id) }).ToList(); ViewBag.PossibleRoles = viewModel; }