Пример #1
0
        public async Task <IActionResult> Edit(DirectorEditModel viewModel)
        {
            if (ModelState.IsValid)
            {
                Director foundDirector = await _directors.GetById(viewModel.Id);

                if (foundDirector != null)
                {
                    ICollection <Movie> linkedMovies = await _directors.GetMovies(foundDirector.Id);

                    foundDirector.Name = viewModel.Name;

                    foreach (AssignedMovies assignedMovie in viewModel.AssignedMovies)
                    {
                        if (assignedMovie.Assigned)
                        {
                            await _directors.AddMovie(foundDirector, assignedMovie.Id);
                        }

                        else if (linkedMovies.Select(movie => movie.Id).ToList().Contains(assignedMovie.Id))
                        {
                            await _directors.RemoveMovie(foundDirector, assignedMovie.Id);
                        }
                    }
                    _directors.Save();
                    ModelState.Clear();
                    return(RedirectToAction($"Detail/{foundDirector.Id}"));
                }
            }

            return(NotFound());
        }
Пример #2
0
        public async Task <IActionResult> Edit(int id, string movieTitle = "")
        {
            //Avoid Null Exception when no search filter passed.
            if (movieTitle == null)
            {
                movieTitle = "";
            }


            Director foundDirector = await _directors.GetById(id);


            if (foundDirector != null)
            {
                DirectorEditModel viewModel = new DirectorEditModel();
                viewModel.Name = foundDirector.Name;
                viewModel.Id   = foundDirector.Id;

                ICollection <Movie> linkedMovies = await _directors.GetMovies(foundDirector.Id);

                ICollection <Movie> allMovies = await _movies.GetAllLight();

                List <AssignedMovies> assignedMovies = allMovies.OrderBy(movie => movie.Title).Where(movie => movie.Title.Contains(movieTitle))
                                                       .Select(movie => new AssignedMovies
                {
                    Id       = movie.Id,
                    Title    = movie.Title,
                    Assigned = linkedMovies.Contains(movie)
                }).ToList();


                viewModel.AssignedMovies = assignedMovies;

                return(View(viewModel));
            }
            return(NotFound());
        }