// GET: /Movies/Create public ActionResult Create() { MovieViewModel movieVM = new MovieViewModel() { Actors = db.Actors.ToList().Select(actor => new SelectListItem() { Text = actor.Name, Value = actor.Id.ToString() }), Actresses = db.Actors.ToList().Select(actor => new SelectListItem() { Text = actor.Name, Value = actor.Id.ToString() }), }; return PartialView("_CreateMovie", movieVM); }
public ActionResult Create(MovieViewModel movieVM) { if (ModelState.IsValid && movieVM != null) { var actorId = Convert.ToInt32(movieVM.LandingMaleRoleName); var actressId = Convert.ToInt32(movieVM.LandingFemaleRoleName); Actor leadingActress = new Actor(); Actor leadingActor = new Actor(); if (actorId > 0) { leadingActor = db.Actors.Find(actorId); if (leadingActor == null) { throw new ArgumentException("Actor does not exists!"); } } if (actressId > 0) { leadingActress = db.Actors.Find(actressId); if (leadingActress == null) { throw new ArgumentException("Actress does not exists!"); } } Movie movie = new Movie() { Title = movieVM.Title, Year = movieVM.Year, LandingFemaleRole = leadingActress, LandingMaleRole = leadingActor }; db.Movies.Add(movie); db.SaveChanges(); return Content(""); } return PartialView("_CreateMovie", movieVM); }
public ActionResult Update(MovieViewModel movieVM) { if (ModelState.IsValid && movieVM != null) { var movie = db.Movies.Find(movieVM.Id); if (movie != null) { if (movie.Title != movieVM.Title) { movie.Title = movieVM.Title; } if (movie.Year != movieVM.Year) { movie.Year = movieVM.Year; } if (movie.LandingFemaleRole.Id != movieVM.LandingFemaleRoleId) { movie.LandingFemaleRole = db.Actors.Find(movieVM.LandingFemaleRoleId); } if (movie.LandingMaleRole.Id != movieVM.LandingMaleRoleId) { movie.LandingMaleRole = db.Actors.Find(movieVM.LandingMaleRoleId); } db.SaveChanges(); return Content(""); } else { throw new ArgumentException("Movie does not exists!"); } } return PartialView("_EditMovie", movieVM); }