public ActionResult Create(UserFormViewModelBase input,string[] UserRoles) { bool nameUsed = _repository.FindByName(input.UserName) != null; if (nameUsed) ModelState.AddModelError("用户名", "该用户名已经有人占用"); if (UserRoles==null) ModelState.AddModelError("角色","必须为用户选择角色"); if (ModelState.IsValid) { var user = new User(); user.UserName = input.UserName; user.LoginName = input.LoginName; user.DepartMentId = input.DepartMentId; user.Password = string.IsNullOrEmpty(input.Password)? "123456":input.Password; user.Activated = input.Activated; user.Roles = new List<Role>(); foreach (var role in UserRoles) { var userrole = _roleRepository.Find(Convert.ToInt32(role)); user.Roles.Add(userrole); } _repository.Add(user); UnitOfWork.Commit(); return RedirectToAction("Manage", "Account"); } input.AvailableRoles = _roleRepository.FindAll().ToList(); ViewData["Departs"] = _departRepository.FindAll().ToList(); return View(input); }
public ActionResult Create() { var model = new UserFormViewModelBase {AvailableRoles = _roleRepository.FindAll().ToList()}; ViewData["Departs"] = _departRepository.FindAll().ToList(); return View(model); }