// 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()); } var genres = db.Genre .Select(g => new SelectListItem { Text = g.Name, Value = g.Id.ToString() }) .ToList(); int genreId = 0; if (movie.Genre != null) { genreId = movie.Genre.Id; } var viewModel = new CreateOrEditMovieViewModel { Genres = genres, Movie = movie, GenreId = genreId }; return(View(viewModel)); }
public ActionResult Edit(CreateOrEditMovieViewModel model) { if (ModelState.IsValid) { db.Entry(model.Movie).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(model)); }
public ActionResult Create(CreateOrEditMovieViewModel model) { if (ModelState.IsValid) { var genre = db.Genre.Find(model.GenreId); model.Movie.Genre = genre; db.Movies.Add(model.Movie); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(model)); }
// GET: Movies/Create public ActionResult Create() { var genres = db.Genre .Select(g => new SelectListItem { Text = g.Name, Value = g.Id.ToString() }) .ToList(); var viewModel = new CreateOrEditMovieViewModel { Genres = genres }; return(View(viewModel)); }