Ejemplo n.º 1
0
        public ActionResult List()
        {
            //construct VM using Linq

            /*
             * IEnumerable<EmplListVM> model = from e in EmplRepoStub.ReadAllEmployees()
             *  join d in DeptRepoStub.ReadAllDepartments()
             *      on e.DepartmentId equals d.Id
             *  select new EmplListVM
             *  {
             *      Name = e.FirstName + " " + e.LastName,
             *      DeptName = d.Name,
             *      PhoneNum = e.PhoneNum,
             *      EmplId = e.Id
             *  };
             */

            //method syntax equivalent
            IEnumerable <EmplListVM> model = EmplRepoStub.ReadAllEmployees()
                                             .Join(
                DeptRepoStub.ReadAllDepartments(),
                empl => empl.DepartmentId,
                dept => dept.Id,
                (empl, dept) => new EmplListVM
            {
                Name     = empl.FirstName + " " + empl.LastName,
                DeptName = dept.Name,
                PhoneNum = empl.PhoneNum,
                EmplId   = empl.Id
            }
                );

            return(View(model));
        }
Ejemplo n.º 2
0
        public ActionResult Edit(int id)
        {
            Employee empl = EmplRepoStub.ReadEmployee(id);

            var model = new EditEmplVM
            {
                EmployeeId  = empl.Id,
                FirstName   = empl.FirstName,
                LastName    = empl.LastName,
                DeptId      = empl.DepartmentId,
                PhoneNum    = empl.PhoneNum,
                Departments = GetDepartmentsSelectListItems()
            };

            return(View(model));
        }
Ejemplo n.º 3
0
        public ActionResult Add(AddEmplVM model)
        {
            if (!ModelState.IsValid)
            {
                //have to manually reload select list item list as nothing is coming back from model data
                model.Departments = GetDepartmentsSelectListItems();
                return(View(model)); //return them to add page on fail
            }

            Employee add = new Employee
            {
                FirstName    = model.FirstName,
                LastName     = model.LastName,
                PhoneNum     = model.PhoneNum,
                DepartmentId = model.DeptId
            };

            EmplRepoStub.CreateEmployee(add);

            return(RedirectToAction("List")); //redirect them to list view on completion
        }
Ejemplo n.º 4
0
        public ActionResult Edit(EditEmplVM model)
        {
            if (!ModelState.IsValid)
            {
                model.Departments = GetDepartmentsSelectListItems();
                return(View(model));
            }

            Employee edit = new Employee
            {
                Id           = model.EmployeeId,
                FirstName    = model.FirstName,
                LastName     = model.LastName,
                PhoneNum     = model.PhoneNum,
                DepartmentId = model.DeptId
            };

            EmplRepoStub.EditEmployee(edit);

            return(RedirectToAction("List"));
        }
Ejemplo n.º 5
0
        public ActionResult Delete(int id)
        {
            EmplRepoStub.DeleteEmployee(id);

            return(RedirectToAction("List"));
        }