示例#1
0
        public ActionResult Edit(ComicBooksEditViewModel viewModel)
        {
            ValidateComicBook(viewModel.ComicBook);

            if (ModelState.IsValid)
            {
                var comicBook = viewModel.ComicBook;

                // Variant 1: updates all fields, regardless of which ones were modified
                _comicBooksRepository.Update(comicBook);

                // Variant 2: fetch current values from DB and update only changed fields via additional model binding pass
                // Create an anonymous object in order to keep the shape of the model the same as the incoming form post data.
                //var comicBookToUpdate = new
                //{
                //    ComicBook = Repository.GetComicBook(comicBook.Id, false)
                //};
                //UpdateModel(comicBookToUpdate); // or TryUpdateModel()
                //Repository.SaveChanges();

                TempData["Message"] = "Your comic book was successfully updated!";

                return(RedirectToAction("Detail", new { id = comicBook.Id }));
            }

            viewModel.Init(Repository);

            return(View(viewModel));
        }
        public ActionResult Edit(ComicBooksEditViewModel viewModel)
        {
            ValidateComicBook(viewModel.ComicBook);

            if (ModelState.IsValid)
            {
                try
                {
                    _comicBooksRepository.Update(viewModel.ComicBook);

                    TempData["Message"] = "Your comic book was successfully updated!";

                    return(RedirectToAction("Detail", new { id = viewModel.ComicBook.Id }));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var entityProperty = ex.Entries.Single().GetDatabaseValues();
                    if (entityProperty != null)
                    {
                        TempData["EditErrors"] = "Your loaded comic was changed by another user";
                        return(RedirectToAction("Edit", new { id = ((ComicBook)entityProperty.ToObject()).Id }));
                    }
                    else
                    {
                        TempData["Error"] = "Your changed comic was already delete by some else";
                        return(RedirectToAction("Index"));
                    }
                }
            }

            viewModel.Init(SeriesRepository, ArtistRepository, RoleRepository);

            return(View(viewModel));
        }
示例#3
0
        /// <summary>
        /// Lists the editable properties for the provided comic book ID
        /// and prompts the user to select a property to edit.
        /// </summary>
        /// <param name="comicBookId">The comic book ID to update.</param>
        private static void UpdateComicBook(int comicBookId)
        {
            _comicBookRepository = new ComicBooksRepository(context);
            ComicBook comicBook = _comicBookRepository.Get(comicBookId);

            string command = CommandListComicBookProperties;

            // If the current command doesn't equal the "Cancel" command
            // then evaluate and process the command.
            while (command != CommandCancel)
            {
                switch (command)
                {
                case CommandListComicBookProperties:
                    ListComicBookProperties(comicBook);
                    break;

                case CommandSave:
                    _comicBookRepository.Update(comicBook);
                    command = CommandCancel;
                    continue;

                default:
                    if (AttemptUpdateComicBookProperty(command, comicBook))
                    {
                        command = CommandListComicBookProperties;
                        continue;
                    }
                    else
                    {
                        ConsoleHelper.OutputLine("Sorry, but I didn't understand that command.");
                    }
                    break;
                }

                // List the available commands.
                ConsoleHelper.OutputBlankLine();
                ConsoleHelper.Output("Commands: ");
                if (EditableProperties.Count > 0)
                {
                    ConsoleHelper.Output("Enter a Number 1-{0}, ", EditableProperties.Count);
                }
                ConsoleHelper.OutputLine("S - Save, C - Cancel", false);

                // Get the next command from the user.
                command = ConsoleHelper.ReadInput("Enter a Command: ", true);
            }

            context.Dispose();
            ConsoleHelper.ClearOutput();
        }
示例#4
0
        public ActionResult Edit(ComicBooksEditViewModel viewModel)
        {
            ValidateComicBook(viewModel.ComicBook);

            if (ModelState.IsValid)
            {
                var comicBook = viewModel.ComicBook;
                _comicBooksRepository.Update(comicBook);
                TempData["Message"] = "Your comic book was successfully updated!";

                return(RedirectToAction("Detail", new { id = comicBook.Id }));
            }

            viewModel.Init(Repository);

            return(View(viewModel));
        }
        public ActionResult Edit(ComicBooksEditViewModel viewModel)
        {
            ValidateComicBook(viewModel.ComicBook);

            if (ModelState.IsValid)
            {
                var comicBook = viewModel.ComicBook;

                try
                {
                    _comicBooksRepository.Update(comicBook);

                    TempData["Message"] = "Your comic book was successfully updated!";

                    return(RedirectToAction("Detail", new { id = comicBook.Id }));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    string message = null;

                    var entityPropertyValues = ex.Entries.Single().GetDatabaseValues();

                    if (entityPropertyValues == null)
                    {
                        message = "The comic book being updated has been deleted by another user. Click the 'Cancel' Button to return to the list page.";

                        viewModel.ComicBookHasBeenDeleted = true;
                    }
                    else
                    {
                        message = "The comic book being updated has already been updated by another user. If you still want to make your changes than click the 'Save' button again. Otherwise click the 'Cancel' button to discard your changes.";

                        comicBook.RowVersion = ((ComicBook)entityPropertyValues.ToObject()).RowVersion;
                    }

                    ModelState.AddModelError(string.Empty, message);
                }
            }

            viewModel.Init(Repository);

            return(View(viewModel));
        }
        public ActionResult Edit(ComicBooksEditViewModel viewModel)
        {
            ValidateComicBook(viewModel.ComicBook);

            if (ModelState.IsValid)
            {
                var comicBook = viewModel.ComicBook;
                try
                {
                    _comicBooksRepository.Update(comicBook);

                    TempData["Message"] = "Your comic book was successfully updated!";
                    return(RedirectToAction("Detail", new { id = comicBook.Id }));
                }
                catch (DbUpdateConcurrencyException e)
                {
                    string message = null;

                    var entityPropertyValues = e.Entries.Single().GetDatabaseValues();

                    if (entityPropertyValues == null)
                    {
                        message = "Another user deleted this comicbook while you were attempting to edit. Click 'Cancel' to return to the list.";

                        viewModel.ComicBookHasBeenDeleted = true;
                    }
                    else
                    {
                        message = "The comicbook was updated by another user. If you still want to save your changes, click 'Save' again. Otherwise click 'Cancel' to discard changes.";

                        comicBook.RowVersion = ((ComicBook)entityPropertyValues.ToObject()).RowVersion;
                    }

                    ModelState.AddModelError(string.Empty, message);
                }
            }

            viewModel.Init(Repository, _seriesRepository, _artistsRepository);

            return(View(viewModel));
        }