Esempio n. 1
0
        public ActionResult Edit(BookViewModel bookViewModel)
        {
            if (ModelState.IsValid)
            {

                var bookToUpdate = _db.Query<Book>()
                    .Include(i => i.Authors)
                    .Include(i => i.Subjects).First(i => i.BookID == bookViewModel.Book.BookID);

                if (TryUpdateModel(bookToUpdate, "Book"))
                {
                    // Update references to author (Add new/Remove old)
                    var newAuthors = _db.Query<Author>().Where(
                        m => bookViewModel.SelectedAuthors.Contains(m.AuthorID)).ToList();
                    var updatedAuthors = new HashSet<int>(bookViewModel.SelectedAuthors);
                    foreach (Author author in _db.Query<Author>())
                    {
                        if (!updatedAuthors.Contains(author.AuthorID))
                        {
                            bookToUpdate.Authors.Remove(author);
                        }
                        else
                        {
                            bookToUpdate.Authors.Add(author);
                        }
                    }

                    // Update references to subjects (Add new/Remove old)
                    var newSubjects = _db.Query<Subject>().Where(
                        s => bookViewModel.SelectedSubjects.Contains(s.SubjectID)).ToList();
                    var updatedSubjects = new HashSet<int>(bookViewModel.SelectedSubjects);

                    foreach (Subject sub in _db.Query<Subject>())
                    {
                        if (!updatedSubjects.Contains(sub.SubjectID))
                        {
                            bookToUpdate.Subjects.Remove(sub);
                        }
                        else
                        {
                            bookToUpdate.Subjects.Add(sub);
                        }
                    }

                    _db.SetState(bookToUpdate, EntityState.Modified);
                    _db.SaveChanges();
                }

                return RedirectToAction("Index");
            }

            PopulateCategoryDropDownList(bookViewModel.Book.CategoryID);
            PopulatePublishersDropDownList(bookViewModel.Book.PublisherID);

            return View(bookViewModel);
        }
Esempio n. 2
0
        // GET: Books/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = _db.Query<Book>()
                .Include(i => i.Authors)
                .Include(i => i.Subjects)
                .First(b => b.BookID == id);

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

            PopulateCategoryDropDownList(book.CategoryID);
            PopulatePublishersDropDownList(book.PublisherID);

            var allAuthors = _db.Query<Author>().Select(o => new SelectListItem
            {
                Text = o.Lastname,
                Value = o.AuthorID.ToString()
            });

            var allSubjects = _db.Query<Subject>().Select(o => new SelectListItem
            {
                Text = o.Name,
                Value = o.SubjectID.ToString()
            });

            var bookViewModel = new BookViewModel()
            {
                Book = book,
                AllAuthors = allAuthors,
                AllSubjects = allSubjects
            };

            var tets = bookViewModel.SelectedSubjects;
            var tets2 = bookViewModel.SelectedAuthors;

            return View(bookViewModel);
        }