public async Task <IActionResult> Create(RegisterViewModel users) { if (ModelState.IsValid) { var loginUser = _userManager.GetUserAsync(HttpContext.User); if (users.fileImage.Length > 0) { using (var memoryStream = new MemoryStream()) { await users.fileImage.CopyToAsync(memoryStream); users.Image = memoryStream.ToArray(); } } var user = new Users { UserName = users.UserName, Email = users.Email, FristName = users.FristName, LastName = users.LastName, NationalCode = users.NationalCode, PhoneNumber = users.PhoneNumber, Image = users.Image, SentEmail = users.SentEmail, SentSMS = users.SentSMS, //**** افزودن کاربر مربوط به کدام شرکته CompanyInfoID = loginUser.Result.CompanyInfoID }; var result = await _userManager.CreateAsync(user, users.Password); if (result.Succeeded) { //**** افزودن سطح دسترسی تعریف شده در نرم افزار foreach (var item in users.RoleID) { UserRoles rol = new UserRoles() { RoleId = item, UserId = user.Id }; _context.UserRoles.Add(rol); } await _context.SaveChangesAsync(); //**** خط زیر خطا میداد فعلا حذف کردم //await _signInManager.SignInAsync(user, isPersistent: false); //_logger.LogInformation(3, "User created a new account with password."); return(RedirectToAction("Index", null)); } } ViewData["RoleID"] = new SelectList(_context.Roles, "Id", "Name", users.RoleID); return(View(users)); }
public async Task <IActionResult> Create([Bind("Id,Name,NormalizedName,ConcurrencyStamp")] Roles roles) { if (ModelState.IsValid) { _context.Add(roles); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(roles)); }
public async Task <IActionResult> Create([Bind("ID,CompanyName,Address,SiteUrl,file")] CompanyInfo CreatViewModel) { if (ModelState.IsValid) { if (CreatViewModel.file != null && CreatViewModel.file.Length > 0) { using (var memoryStream = new MemoryStream()) { await CreatViewModel.file.CopyToAsync(memoryStream); CreatViewModel.Icon = memoryStream.ToArray(); } } _context.Add(CreatViewModel); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(CreatViewModel)); }
public async Task <IActionResult> Create([Bind("UserId,RoleId")] UserRoles userRoles) { try { var loginUser = _userManager.GetUserAsync(HttpContext.User); if (ModelState.IsValid) { if (userRoles.UserId == null) { return(View()); } if (userRoles.RoleId == null) { var lstUser = _context.Users.Where(i => i.CompanyInfoID == loginUser.Result.CompanyInfoID && i.Id == userRoles.UserId); var lstUserRol = _context.UserRoles.Where(i => i.UserId == userRoles.UserId); ViewData["UserID"] = new SelectList(lstUser, "Id", "FullName"); return(View()); } _context.Add(userRoles); var result = await _context.SaveChangesAsync(); } ViewData["RoleID"] = new SelectList(_context.Roles, "Id", "Name"); ViewData["UserID"] = new SelectList(_context.Users.Where(i => i.CompanyInfoID == loginUser.Result.CompanyInfoID), "Id", "FullName"); return(RedirectToAction("Index", null)); } catch (DbUpdateException ee) { return(NotFound("Can not Creat Duplicate Role for one person")); } catch (Exception e) { return(NotFound(e)); } }