Example #1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Specialisation")] ITStaff iTStaff)
        {
            if (id != iTStaff.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(iTStaff);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ITStaffExists(iTStaff.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(iTStaff));
        }
        public ActionResult Create(ITStaffViewModel model)
        {
            if (ModelState.IsValid)
            {
                var ITstaff = new ITStaff
                {
                    UserName        = model.UserName,
                    Email           = model.Email,
                    FirstName       = model.FirstName,
                    LastName        = model.LastName,
                    Department      = "IT Department",
                    JobTitle        = model.JobTitle,
                    Mobile          = model.Mobile,
                    ExtensionNumber = model.ExtensionNumber,
                    OfficeNumber    = model.OfficeNumber,
                    Speciality      = model.Speciality,
                    StartingDate    = model.StartingDate,
                    Position        = model.Position,
                    IsManager       = model.IsManager,
                };

                var result = UserManager.Create(ITstaff, model.Password);

                if (result.Succeeded && model.IsManager == true)
                {
                    var roleResult = UserManager.AddToRoles(ITstaff.Id, "ITManager");

                    if (roleResult.Succeeded)
                    {
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, roleResult.Errors.First());
                        return(View());
                    }
                }
                if (result.Succeeded && model.IsManager == false)
                {
                    var roleResult = UserManager.AddToRoles(ITstaff.Id, "ITStaff");

                    if (roleResult.Succeeded)
                    {
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, roleResult.Errors.First());
                        return(View());
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, result.Errors.First());
                    return(View());
                }
            }

            return(View());
        }
Example #3
0
        public async Task <IActionResult> Create([Bind("Id,Name,Specialisation")] ITStaff iTStaff)
        {
            if (ModelState.IsValid)
            {
                _context.Add(iTStaff);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(iTStaff));
        }