コード例 #1
0
        public ActionResult Create(EmployeeEditVM model)
        {
            if (ModelState.IsValid)
            {
                var currentTeam = db.Teams.Find(model.SelectedTeamId);
                var alreadyExists = currentTeam.Employees.Any(x => x.EmployeeNumber == model.EmployeeNumber);
                if (alreadyExists )
                {
                    ModelState.AddModelError("EmployeeNumber", "Person Already Exists with that Employee Number on the Team.");
                    model.AvailableTeams = new SelectList(db.Teams.ToList(), "Id", "Name");
                    return View(model);
                }

                 var   employee = new Employee
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    EmployeeNumber = model.EmployeeNumber,
                    MemberOf = currentTeam
                };

                db.Employees.Add(employee);
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            model.AvailableTeams = new SelectList(db.Teams.ToList(), "Id", "Name");
            return View(model);
        }
コード例 #2
0
        // GET: Employees/Create
        public ActionResult Create()
        {
            var model = new EmployeeEditVM
            {
                SelectedTeamId = 0,
                AvailableTeams = new SelectList(db.Teams.ToList(), "Id", "Name")
            };

            return View(model);
        }
コード例 #3
0
        public ActionResult Edit(EmployeeEditVM model)
        {
            if (ModelState.IsValid)
            {
                var employee = db.Employees.Find(model.Id);
                employee.FirstName = model.FirstName;
                employee.LastName = model.LastName;
                employee.EmployeeNumber = model.EmployeeNumber;
                employee.MemberOf = db.Teams.Find(model.SelectedTeamId);

                db.SaveChanges();

                return RedirectToAction("Index");
            }

            model.AvailableTeams = new SelectList(db.Teams.ToList(), "Id", "Name");
            return View(model);
        }
コード例 #4
0
        // GET: Employees/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Employee employee = db.Employees.Find(id);

            if (employee == null)
            {
                return HttpNotFound();
            }

            var model = new EmployeeEditVM
            {
                SelectedTeamId = employee.MemberOf?.Id ?? 0,
                Id = employee.Id,
                FirstName = employee.FirstName,
                LastName = employee.LastName,
                EmployeeNumber = employee.EmployeeNumber,
                AvailableTeams = new SelectList(db.Teams.ToList(), "Id", "Name")
            };

            return View(model);
        }