コード例 #1
0
        public ActionResult CreateFacultyHead(CreateFacultyHeadViewModel model)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser _tempUser = new ApplicationUser();
                try
                {
                    ApplicationDbContext dbContext = new ApplicationDbContext();
                    var user = new ApplicationUser {
                        UserName = model.Email, Email = model.Email
                    };
                    string password = ApplicationHelper.GeneratePassword(5);
                    var    result   = UserManager.Create(user, password);
                    if (result.Succeeded)
                    {
                        _tempUser = user;
                        UserManager.AddToRole(user.Id, "dephead");
                        Worker worker = new Worker();
                        worker.Firstname     = model.Firstname;
                        worker.MiddleName    = model.MiddleName != null ? model.MiddleName : "";
                        worker.LastName      = model.LastName;
                        worker.UserId        = user.Id;
                        worker.ScienceDegree = model.ScienceDegree;
                        worker.Status        = (WorkerStatus)int.Parse(model.Status);
                        worker.isItProfessor = false;

                        dbContext.Workers.Add(worker);
                        dbContext.SaveChanges();
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
                catch (Exception)
                {
                    var _res = UserManager.Delete(_tempUser);
                    AddErrors(_res);
                }

                model.DropdownData = new List <SelectListItem>();
                foreach (WorkerStatus item in Enum.GetValues(typeof(WorkerStatus)))
                {
                    model.DropdownData.Add(new SelectListItem
                    {
                        Text  = ApplicationHelper.GetWorkerStatusDesc(item),
                        Value = ((int)item).ToString()
                    });
                }
            }

            return(View(model));
        }
コード例 #2
0
        public ActionResult CreateFacultyHead()
        {
            CreateFacultyHeadViewModel model = new CreateFacultyHeadViewModel();

            model.DropdownData = new List <SelectListItem>();
            foreach (WorkerStatus item in Enum.GetValues(typeof(WorkerStatus)))
            {
                model.DropdownData.Add(new SelectListItem
                {
                    Text  = ApplicationHelper.GetWorkerStatusDesc(item),
                    Value = ((int)item).ToString()
                });
            }
            return(View(model));
        }