コード例 #1
0
        // GET: Instructor/Create
        public ActionResult Create()
        {
            var repository = this.UoW.GetRepository<Course>();
            if (repository == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }

            var viewModel = new InstructorViewModel
            {
                HireDate = DateTime.Now,
                Courses = repository.GetAll().Select(x => new CourseViewModel { Id = x.Id, Name = x.Title, IsSelected = false }).ToList()
            };

            return View(viewModel);
        }
コード例 #2
0
        public ActionResult Create(InstructorViewModel instructorViewModel)
        {
            var repository = this.UoW.GetRepository<Instructor>();
            if (repository == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }

            if (ModelState.IsValid)
            {
                //
                // If a location is given, need to create an office assignment record
                //
                var officeAssignment = string.IsNullOrEmpty(instructorViewModel.Location) ? null :
                    new OfficeAssignment { Location = instructorViewModel.Location };

                var instructor = new Instructor
                {
                    FirstName = instructorViewModel.FirstName,
                    LastName = instructorViewModel.LastName,
                    HireDate = instructorViewModel.HireDate,
                    OfficeAssignment = officeAssignment,
                    Courses = instructorViewModel.Courses.Where(x => x.IsSelected).Select(x => new Course { Id = x.Id }).ToList()
                };

                this.UoW.Commit(() =>
                {
                    repository.Add(instructor);
                    ////
                    //// Since this is a new entity (parent entity), the courses (the child entities) will also be considered as new entities. To avoid adding new courses, need to inform EF, that the course entities state is "Unchanged"
                    ////
                    //var trackedInstructorEntity = this.UoW.conte.Entry(instructor);
                    //trackedInstructorEntity.State = EntityState.Added;
                    //trackedInstructorEntity.Entity.Courses.ForEach(x => db.Entry(x).State = EntityState.Unchanged);
                });

                return RedirectToAction("Index");
            }

            return View(instructorViewModel);
        }
コード例 #3
0
        public ActionResult EditPost(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var instructors = this.UoW.Get<Instructor>(x => x.Id == id);
            if (instructors == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }

            var instructorToUpdate = instructors
                .Include(x => x.OfficeAssignment)
                .Include(x => x.Courses)
                .FirstOrDefault();

            if (instructorToUpdate == null)
            {
                return new HttpNotFoundResult();
            }

            var instructorViewModel = new InstructorViewModel();

            try
            {
                if (TryUpdateModel(instructorViewModel, new[] { "Id", "FirstName", "LastName", "HireDate", "Location", "Courses" }))
                {
                    var updatedInstructor = new Instructor
                    {
                        Id = instructorViewModel.Id,
                        FirstName = instructorViewModel.FirstName,
                        LastName = instructorViewModel.LastName,
                        HireDate = instructorViewModel.HireDate,
                        Courses =
                            instructorViewModel.Courses == null
                                ? new List<Course>()
                                : instructorViewModel.Courses.Where(x => x.IsSelected)
                                    .Select(x => new Course { Id = x.Id })
                                    .ToList(),

                        OfficeAssignment =
                            new OfficeAssignment
                            {
                                InstructorId = instructorViewModel.Id,
                                Location = instructorViewModel.Location
                            }
                    };

                    this.UoW.Commit(() =>
                    {
                        var repository = this.UoW.GetRepository<Instructor>();
                        if (repository != null)
                        {
                            repository.Update(updatedInstructor);
                        }
                    });

                    //var instructorDbEntity = db.Entry(instructorToUpdate);

                    //var instructorEntity = instructorDbEntity.Entity;
                    //instructorEntity.FirstName = instructorViewModel.FirstName;
                    //instructorEntity.LastName = instructorViewModel.LastName;
                    //instructorEntity.HireDate = instructorViewModel.HireDate;
                    //instructorEntity.Courses.Clear();

                    //if (instructorViewModel.Courses != null)
                    //{
                    //    instructorViewModel.Courses.Where(x => x.IsSelected)
                    //        .ForEach(x => instructorEntity.Courses.Add(db.Courses.Find(x.Id)));
                    //}

                    //if (string.IsNullOrEmpty(instructorViewModel.Location))
                    //{
                    //    var currentOfficeAssignment = db.OfficeAssignments.FirstOrDefault(x => x.InstructorId == instructorToUpdate.Id);

                    //    if (currentOfficeAssignment != null)
                    //    {
                    //        db.Entry(currentOfficeAssignment).State = EntityState.Deleted;
                    //    }
                    //}
                    //else
                    //{
                    //    if (instructorEntity.OfficeAssignment == null)
                    //    {
                    //        instructorEntity.OfficeAssignment = new OfficeAssignment
                    //        {
                    //            Location = instructorViewModel.Location
                    //        };
                    //    }
                    //    else
                    //    {
                    //        instructorEntity.OfficeAssignment.Location = instructorViewModel.Location;
                    //    }

                    //}

                    //instructorDbEntity.State = EntityState.Modified;
                    //db.SaveChanges();

                    return RedirectToAction("Index");
                }
            }
            catch (Exception exception)
            {

            }

            return View("Edit", id);
        }
コード例 #4
0
        // GET: Instructor/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var instructors = this.UoW.Get<Instructor>();
            if (instructors == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }

            var instructor = instructors
                .Include(x => x.OfficeAssignment)
                .Include(x => x.Courses)
                .FirstOrDefault(x => x.Id == id);

            //var instructor = db.Instructors
            //    .Include(x => x.OfficeAssignment)
            //    .Include(x => x.Courses)
            //    .FirstOrDefault(x => x.Id == id);

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

            var instructorCourses = instructor.Courses.ToList();

            var coursesTaught = this.UoW.Get<Course>()
                .ToList()
                .Select(x => new CourseViewModel
                {
                    Id = x.Id,
                    Name = x.Title,
                    IsSelected = instructorCourses.Any(y => y.Title == x.Title)
                })
                .ToList();

            //var coursesTaught = db.Courses.ToList().Select(x => new CourseViewModel
            //{
            //    Id = x.Id,
            //    Name = x.Title,
            //    IsSelected = instructorCourses.Any(y => y.Title == x.Title)
            //}).ToList();

            var viewModel = new InstructorViewModel
            {
                Id = instructor.Id,
                FirstName = instructor.FirstName,
                LastName = instructor.LastName,
                HireDate = instructor.HireDate,
                Location = instructor.OfficeAssignment == null ? string.Empty : instructor.OfficeAssignment.Location,
                Courses = coursesTaught
            };

            return View(viewModel);
        }