//GET: Movies/Details public ActionResult Details(int id) { var movie = _context.Movies.SingleOrDefault(m => m.Id == id); if (movie == null) { return(HttpNotFound()); } var listActorsJoin = _context.MovieActors.Where(m => m.MovieId == id).ToList(); var listOfActors = new List <Actor>(); foreach (var actorId in listActorsJoin) { var actor = _context.Actors.Where(a => a.id == actorId.ActorId).SingleOrDefault(); listOfActors.Add(actor); } var viewModel = new AddActorViewModel { Movie = movie, Actors = listOfActors }; return(View(viewModel)); }
// GET: Movies/Random public ActionResult Random() { //get last id and choose random number between 1 and last id. var lastId = _context.Movies.OrderByDescending(m => m.Id).First().Id; Random r = new Random(); int id = r.Next(1, lastId); var movie = _context.Movies.SingleOrDefault(m => m.Id == id); if (movie == null) { return(HttpNotFound()); } var listActorsJoin = _context.MovieActors.Where(m => m.MovieId == id).ToList(); var listOfActors = new List <Actor>(); foreach (var actorId in listActorsJoin) { var actor = _context.Actors.Where(a => a.id == actorId.ActorId).SingleOrDefault(); listOfActors.Add(actor); } var viewModel = new AddActorViewModel { Movie = movie, Actors = listOfActors }; return(View(viewModel)); }
//GET: Movies/AddActors public ActionResult AddActors(int id) { var movie = _context.Movies.SingleOrDefault(m => m.Id == id); if (movie == null) { return(HttpNotFound()); } var viewModel = new AddActorViewModel { Movie = movie, Actors = _context.Actors.ToList() }; return(View(viewModel)); }
public IActionResult AddActor(AddActorViewModel vM) { if (!this.ModelState.IsValid) { return(View()); } var actors = this.actorsService.GetAll().ToList(); foreach (var actor in actors) { if (actor.FirstName.Equals(vM.FirstName, StringComparison.InvariantCultureIgnoreCase) && actor.LastName.Equals(vM.LastName, StringComparison.InvariantCultureIgnoreCase)) { ViewData["ActorExists"] = "An actor with this name already exists!"; return(View(vM)); } } this.actorsService.AddActor(vM.FirstName, vM.LastName, vM.Age); return(RedirectToAction("Actors", "ManageActors")); }
public AddActor() { InitializeComponent(); DataContext = new AddActorViewModel(this); }