Esempio n. 1
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);
        }
Esempio n. 2
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);
        }