Beispiel #1
0
 public ViewResult AddUser()
 {
     //ViewBag.msl = new MultiSelectList(roles, "Id", "Name");
     UserView newUser = new UserView {Login = "******", Name = "Кислицин Никита", Password = "******", Email = "*****@*****.**"};
     newUser.RolesList = userRepository.Roles.ToList();
     return View("EditUser", newUser);
 }
Beispiel #2
0
        public ActionResult AddUser(UserView newUser, List<int> SelectedRoles)
        {
            var roles = new List<Role>();
            var allRoles = userRepository.Roles;

            foreach (int id in SelectedRoles)
            {
                var role = allRoles.FirstOrDefault(x => x.Id == id);
                roles.Add(role);
            }

            //newUser.Roles = newUser.SelectedRoles.Items as IEnumerable<Role>;
            newUser.Roles = roles;

            //newUser.Roles = new Collection<Role>() { userRepository.Roles.FirstOrDefault(x => x.Name == "User") };
            if (!ModelState.IsValid)
            {
                return View("EditUser", newUser);
            }

            //TODO:* Mapping [DONE]
            var user = (User) AutoMapper.Map(newUser, typeof (UserView), typeof (User));
            userRepository.SaveUser(user);
            return RedirectToAction("Users");
        }