// GET: UserMng/Form/:id public ActionResult Form(string id) { Models.UserDetailVM model = null; if (id != null) { var user = this.aspNetUserService.Get(id); if (user != null) { model = new Models.UserDetailVM { UserName = user.UserName, FullName = user.FullName, isActive = user.isActive, PhoneNumber = user.PhoneNumber, Email = user.Email, BrandId = user.BrandID, Id = user.Id, }; var userRoles = UserManager.GetRoles(user.Id).ToArray(); ViewBag.userRoles = userRoles; if (userRoles.Length > 0) { model.Role = userRoles[0]; } } } ViewBag.brandList = BrandController.GetBrandList(); ViewBag.roleList = UserMngController.GetRoleList(); return(View(model)); }
// POST: UserMng/Update public async System.Threading.Tasks.Task <ActionResult> Update(Models.UserDetailVM model) { if (ModelState.IsValid) { /*Update custom fields*/ var user = aspNetUserService .Get(a => a.Id == model.Id) .FirstOrDefault(); if (model.Role.CompareTo("System Admin") == 0) { model.BrandId = 39; } if (user != null) { user.BrandID = model.BrandId; user.FullName = model.FullName; user.isActive = model.isActive; await this.aspNetUserService.UpdateAsync(user); /*Update user role*/ //Remove from other Roles var userRoles = UserManager.GetRoles(user.Id).ToArray(); if (userRoles.Length > 0) { UserManager.RemoveFromRole(user.Id, userRoles[0]); } //Add to new Role UserManager.AddToRoles(user.Id, new string[] { model.Role }); Session.Clear(); Session["UPDATE_RESULT"] = true; return(new ContentResult { Content = string.Format("<script type='text/javascript'>window.parent.location.href = '{0}';</script>", Url.Action("Index", "UserMng")), ContentType = "text/html" }); } ; } // If we got this far, something failed, redisplay form ViewBag.brandList = BrandController.GetBrandList(); ViewBag.roleList = UserMngController.GetRoleList(); return(View("Form", model)); }
// POST: UserMng/Add public async System.Threading.Tasks.Task <ActionResult> Add(Models.UserDetailVM model) { if (ModelState.IsValid) { if (model.Role.CompareTo("System Admin") == 0) { model.BrandId = 39; } var user = new Wisky.Models.ApplicationUser { UserName = model.UserName, Email = model.Email, FullName = model.FullName, PhoneNumber = model.PhoneNumber, BrandId = model.BrandId, isActive = model.isActive, }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { Session.Clear(); Session["ADD_RESULT"] = true; UserManager.AddToRoles(user.Id, new string[] { model.Role }); return(new ContentResult { Content = string.Format("<script type='text/javascript'>window.parent.location.href = '{0}';</script>", Url.Action("Index", "UserMng")), ContentType = "text/html" }); } } // If we got this far, something failed, redisplay form ViewBag.brandList = BrandController.GetBrandList(); ViewBag.roleList = UserMngController.GetRoleList(); return(View("Form", model)); }