Пример #1
0
        // GET: Films/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Film film = db.Films.Find(id);

            if (film == null)
            {
                return(HttpNotFound());
            }

            //new view model object
            FilmPageViewModel filmPage = new FilmPageViewModel();

            //get the current film and assign to view model
            filmPage.Film = film;
            //populate the Reviews list for the view model by matching all reviews
            //where the film id matches
            filmPage.Reviews = db.Reviews.Where(x => x.FilmID == film.FilmID).ToList();

            //for actors, first we need to get all related records in the join table
            IList <Acting> actorLinks = db.Actings.Where(x => x.FilmId == film.FilmID).ToList();
            //then we'll construct a list of the person records to match
            IList <Person> actors = new List <Person>();

            //here we loop through the acting records in the join table
            foreach (Acting a in actorLinks)
            {
                //and add to the list of actors the matching person record for each
                actors.Add(db.Persons.Where(x => x.PersonId == a.PersonId).Single());
            }
            //once populated, we can assign the list of people as actors to the view model
            filmPage.Actors = actors;
            //here we will use a LINQ query to get the average review score for reviews
            //related to this film - the additional ? symbols are if there is a null result
            //if so we set to 0
            ViewBag.AverageReview =
                db.Reviews.Where(x => x.FilmID == film.FilmID)
                .Average(x => (double?)x.ReviewRating) ?? 0;

            //return the view model to use
            return(View(filmPage));
        }
Пример #2
0
 private void OnPageLoaded(object sender, RoutedEventArgs e)
 {
     this.ViewModel = DataContext as FilmPageViewModel;
     ViewModel.CallService();
 }