public async Task <ActionResult> Register(RegisterViewModel model, IEnumerable <string> Groups) { bool x = true; ViewBag.Current = "Users"; ViewBag.DepartmentID = new SelectList(DepartmentListDisplay.CreateDepartmentListDisplay(), "Id", "Name"); ViewBag.JobTitleId = new SelectList(db.JobTitles.ToList(), "Id", "Name"); ViewBag.Role = new SelectList(db.Roles.Where(a => !a.Name.Equals("Master")).ToList(), "Name", "Name", model.Role); ViewBag.Groups = new SelectList(db.Groups.ToList(), "Id", "Name"); if (ModelState.IsValid) { if (db.Users.Any(a => a.UserName.Equals(model.UserName, StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("UserName", "اسم المستخدم موجود مسبقاً يرجى اعادة الإدخال"); x = false; } if (CheckJobTitleDepartment.CheckJobTitleDepartmentCreateUser(model.DepartmentID, model.JobTitleId) == false) { ModelState.AddModelError("JobTitleId", "عددالأعضاء للقسم بالنسبة للمسمى الوظيفي وصل للحد الأعظمي"); x = false; } if (!string.IsNullOrEmpty(model.Email)) { if (db.Users.Any(a => a.Email.Equals(model.Email, StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("Email", "لا يمكن أن يكون البريد الإلكتروني مكرر، يرجى إعادةالإدخال"); x = false; } } if (x == false) { return(View(model)); } var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, FullName = model.FullName, Gender = model.Gender, DepartmentId = model.DepartmentID, JobTitleId = model.JobTitleId, CreatedAt = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss"), CreatedById = this.User.Identity.GetUserId(), RoleName = model.Role }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await UserManager.AddToRoleAsync(user.Id, model.Role); //Add User To Groups if (Groups != null) { string UserId = User.Identity.GetUserId(); Notification notification = null; string NotificationTime = string.Empty; string GroupName = string.Empty; foreach (string User_Group_Id in Groups) { var UserGroup = new UserGroup() { UserId = user.Id, GroupId = Convert.ToInt32(User_Group_Id), CreatedAt = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss"), CreatedById = this.User.Identity.GetUserId() }; NotificationTime = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss"); db.UsersGroups.Add(UserGroup); GroupName = db.Groups.Find(UserGroup.GroupId).Name; notification = new Notification() { CreatedAt = NotificationTime, Active = false, UserId = user.Id, Message = "تم إضافتك إلى المجموعة :" + GroupName , NotificationOwnerId = UserId }; db.Notifications.Add(notification); } } db.SaveChanges(); return(RedirectToAction("Index", new { @Id = "CreateSuccess" })); } // AddErrors(result); } return(View(model)); }