Пример #1
0
        public ActionResult Create(MovieViewModel model)
        {
            var maleActor = db.Actors.Find(model.LeadingMaleRoleId);
            var femaleActor = db.Actors.Find(model.LeadingFemaleRoleId);
            var studio = db.Studios.Find(model.StudioId);

            if (maleActor != null && femaleActor != null && studio != null)
            {
                var movie = new Movie();
                movie.Title = model.Title;
                movie.Year = model.Year;
                movie.Director = model.Director;
                movie.LeadingMaleRole = maleActor;
                movie.LeadingFemaleRole = femaleActor;
                movie.Studio = studio;

                if (ModelState.IsValid)
                {
                    db.Movies.Add(movie);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }

            ViewBag.MaleActors = db.Actors.ToList().Where(a => a.Gender == Gender.Male).Select(a => new SelectListItem { Text = a.Name, Value = a.Id.ToString() });
            ViewBag.FemaleActors = db.Actors.ToList().Where(a => a.Gender == Gender.Female).Select(a => new SelectListItem { Text = a.Name, Value = a.Id.ToString() });
            ViewBag.Studios = db.Studios.ToList().Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() });

            return PartialView("_CreateMovie", model);
        }
        // GET 
        public ActionResult EditMovie(string title, string director, int year, int id)
        {
            var model = new MovieViewModel()
            {
                Title = title,
                Director = director,
                Year = year,
                Id = id
            };

            return this.PartialView("_PartialEditMovie", model);
        }
        public ActionResult Create(MovieViewModel model)
        {
            if (ModelState.IsValid)
            {
                var newMovie = new Movie()
                {
                    Title = model.Title,
                    Year = model.Year,
                    StudioName = model.StudioName,
                    StudioAddress = model.StudioAddress,
                    LeadingFemaleRole = new Actor() { Name = model.LeadingFemaleRole, Age = model.LeadingFemaleRoleAge },
                    LeadingMaleRole = new Actor() { Name = model.LeadingMaleRole, Age = model.LeadingMaleRoleAge }
                };

                this.movies.Add(newMovie);
                this.movies.SaveChanges();
                return this.RedirectToAction("Index");
            }

            return View(model);
        }
        public ActionResult Edit(MovieViewModel model)
        {
            if (ModelState.IsValid)
            {
                var movieForEdit = this.movies.GetById(model.Id);

                movieForEdit.Title = model.Title;
                movieForEdit.Year = model.Year;
                movieForEdit.StudioName = model.StudioName;
                movieForEdit.StudioAddress = model.StudioAddress;
                movieForEdit.LeadingMaleRole.Name = model.LeadingMaleRole;
                movieForEdit.LeadingFemaleRole.Name = model.LeadingFemaleRole;
                movieForEdit.LeadingMaleRole.Age = model.LeadingMaleRoleAge;
                movieForEdit.LeadingFemaleRole.Age = model.LeadingFemaleRoleAge;

                this.movies.SaveChanges();
                return this.RedirectToAction("Index");
            }

            return View(model);
        }
Пример #5
0
        // GET: /Movies/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Movie movie = db.Movies.Find(id);
            if (movie == null)
            {
                return HttpNotFound();
            }

            ViewBag.MaleActors = db.Actors.ToList().Where(a => a.Gender == Gender.Male).Select(a => new SelectListItem { Text = a.Name, Value = a.Id.ToString() });
            ViewBag.FemaleActors = db.Actors.ToList().Where(a => a.Gender == Gender.Female).Select(a => new SelectListItem { Text = a.Name, Value = a.Id.ToString() });
            ViewBag.Studios = db.Studios.ToList().Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() });

            var movieViewModel = new MovieViewModel()
            {
                Title = movie.Title,
                Director = movie.Director,
                Year = movie.Year,
                LeadingFemaleRoleId = movie.LeadingFemaleRole.Id,
                LeadingMaleRoleId = movie.LeadingMaleRole.Id,
                StudioId = movie.Studio.Id
            };

            return PartialView("_EditMovie", movieViewModel);
        }