コード例 #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            CinephileDbEntities db = new CinephileDbEntities();
            var movieId            = Request.Params["Id"];
            // Warning: Possible n+1 queries problem
            var movie = db.Movies
                        .Where(m => m.Id.ToString() == movieId)
                        .Select(m => new
            {
                PosterPath  = m.PosterPath,
                Title       = m.Title,
                Storyline   = m.Storyline,
                ReleaseDate = m.ReleseDate,
                TimeLength  = m.RunningTime,
                Language    = m.Language,
                Genres      = m.Genres,
                Directors   = m.Directors,
                Actors      = m.Actors,
                Reviews     = m.Reviews,
                Ratings     = m.Ratings,
                Countries   = m.Countries
            })
                        .FirstOrDefault();

            //var movie = db.Movies.Find(movieId);

            this.PageTitle.Text      = movie.Title;
            this.MovieImage.ImageUrl = movie.PosterPath;
            this.StoryLine.Text      = movie.Storyline;
            this.ReleaseDate.Text    = movie.ReleaseDate.ToShortDateString();
            this.Language.Text       = movie.Language.Name;
            this.Length.Text         = movie.TimeLength.ToString() + " min";
            double rating = 0;

            foreach (var rate in movie.Ratings)
            {
                rating += rate.RatingValue;
            }

            rating = rating / movie.Ratings.Count;
            if (double.IsNaN(rating))
            {
                rating = 0;
            }
            this.AddRating(rating);

            if (movie.Countries.Count > 1)
            {
                this.Countries.Text = "Countries";
            }
            else
            {
                this.Countries.Text = "Country";
            }
            this.RepeaterCountries.DataSource = movie.Countries;

            if (movie.Genres.Count > 1)
            {
                this.Genres.Text = "Genres";
            }
            else
            {
                this.Genres.Text = "Genre";
            }
            this.RepeaterGenres.DataSource    = movie.Genres;
            this.RepeaterDirectors.DataSource = movie.Directors;
            var dt = this.ConvertToDataTable(movie.Actors.ToList());

            this.GridViewActors.DataSource = dt;

            Page.DataBind();
        }
コード例 #2
0
        protected void ButtonCreateMovie_Click(object sender, EventArgs e)
        {
            string title = this.TextBoxMovieTitle.Text;

            if (string.IsNullOrWhiteSpace(title))
            {
                this.ValidationSummaryMessages.Text = "Correct Title is required!";
                return;
            }

            string storyline = this.TextBoxMovieStoryline.Text;

            if (string.IsNullOrWhiteSpace(storyline))
            {
                this.ValidationSummaryMessages.Text = "Correct Storyline is required!";
                return;
            }

            string   releaseDateString = this.TextBoxMovieReleaseDate.Text;
            DateTime releaseDate       = new DateTime();

            if (DateTime.TryParse(releaseDateString, out releaseDate) == false)
            {
                this.ValidationSummaryMessages.Text = "Correct Release date is required!";
                return;
            }

            string runningTimeString = this.TextBoxMovieRunningTime.Text;
            int    runningTime;

            if (int.TryParse(runningTimeString, out runningTime) == false)
            {
                this.ValidationSummaryMessages.Text = "Correct Running time is required!";
                return;
            }

            int languageId;

            if (int.TryParse(this.DropDownListMovieLanguages.SelectedValue, out languageId) == false)
            {
                this.ValidationSummaryMessages.Text = "Correct Language is required!";
                return;
            }

            CinephileDbEntities db = new CinephileDbEntities();
            var language           = db.Languages.FirstOrDefault(lang => lang.Id == languageId);

            if (language == null)
            {
                this.ValidationSummaryMessages.Text = "Correct Language is required!";
                return;
            }

            int[] selectedCountriesIndeces = this.ListBoxMovieCountries.GetSelectedIndices();
            if (selectedCountriesIndeces.Length < 1)
            {
                this.ValidationSummaryMessages.Text = "Correct Country is required!";
                return;
            }

            int[] selectedGenresIndeces = this.ListBoxMovieGenres.GetSelectedIndices();
            if (selectedGenresIndeces.Length < 1)
            {
                this.ValidationSummaryMessages.Text = "Correct Gener is required!";
                return;
            }

            var newMovie = new Movie();

            newMovie.LanguageId  = languageId;
            newMovie.ReleseDate  = releaseDate;
            newMovie.RunningTime = runningTime;
            newMovie.Storyline   = storyline;
            newMovie.Title       = title;

            var     allCountries        = this.ListBoxMovieCountries.Items;
            string  currSelectedCountry = string.Empty;
            Country currCountry;

            foreach (int country in selectedCountriesIndeces)
            {
                currSelectedCountry = allCountries[country].Value;
                currCountry         = db.Countries.FirstOrDefault(c => c.Id.ToString() == currSelectedCountry);

                if (currCountry == null)
                {
                    this.ValidationSummaryMessages.Text = "Invalid Country entered!";
                    return;
                }

                newMovie.Countries.Add(currCountry);
            }

            var    allGenres         = this.ListBoxMovieGenres.Items;
            string currSelectedGener = string.Empty;
            Genre  currGenre;

            foreach (int gener in selectedGenresIndeces)
            {
                currSelectedGener = allGenres[gener].Value;
                currGenre         = db.Genres.FirstOrDefault(g => g.Id.ToString() == currSelectedGener);

                if (currGenre == null)
                {
                    this.ValidationSummaryMessages.Text = "Invalid Genre entered!";
                    return;
                }

                newMovie.Genres.Add(currGenre);
            }

            if (FileUploadMoviePoster.HasFile)
            {
                try
                {
                    //if (FileUploadMoviePoster.PostedFile.ContentType == "image/*")
                    string contentType = FileUploadMoviePoster.PostedFile.ContentType;
                    if (contentType == "image/jpeg" || contentType == "image/jpg" ||
                        contentType == "image/png" || contentType == "image/gif")
                    {
                        string filename = Path.GetFileName(FileUploadMoviePoster.FileName);
                        FileUploadMoviePoster.SaveAs(Server.MapPath("~/Images/") + filename);
                    }
                    else
                    {
                        this.ValidationSummaryMessages.Text = "Upload status: Only Image files are accepted!";
                        return;
                    }
                }
                catch (Exception ex)
                {
                    this.ValidationSummaryMessages.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                    return;
                }

                newMovie.PosterPath = "~/Images/" + this.FileUploadMoviePoster.FileName;
            }

            db.Movies.Add(newMovie);
            db.SaveChanges();

            Response.Redirect("~/MovieDetails.aspx?id=" + newMovie.Id);
        }
コード例 #3
0
        public IQueryable <AspNetRole> RolesDropDownList_GetData()
        {
            CinephileDbEntities db = new CinephileDbEntities();

            return(db.AspNetRoles.OrderBy(m => m.Name.ToLower()));
        }
コード例 #4
0
        protected void ButtonEditMovie_Click(object sender, EventArgs e)
        {
            string currMovieId     = Request.Params["id"];
            CinephileDbEntities db = new CinephileDbEntities();
            Movie currMovie        = db.Movies.FirstOrDefault(m => m.Id.ToString() == currMovieId);

            if (currMovie == null)
            {
                this.ValidationSummaryMessages.Text = "Movie with id:" + currMovieId + " was not found!";
                return;
            }

            string title = this.TextBoxNewMovieTitle.Text;

            if (!string.IsNullOrWhiteSpace(title))
            {
                currMovie.Title = title;
            }

            string storyline = this.TextBoxNewMovieStoryline.Text;

            if (!string.IsNullOrWhiteSpace(storyline))
            {
                currMovie.Storyline = storyline;
            }

            string   releaseDateString = this.TextBoxNewMovieReleaseDate.Text;
            DateTime releaseDate       = new DateTime();

            if (DateTime.TryParse(releaseDateString, out releaseDate))
            {
                currMovie.ReleseDate = releaseDate;
            }

            string runningTimeString = this.TextBoxNewMovieRunningTime.Text;
            int    runningTime;

            if (int.TryParse(runningTimeString, out runningTime))
            {
                currMovie.RunningTime = runningTime;
            }

            int languageId;

            if (int.TryParse(this.DropDownListNewMovieLanguages.SelectedValue, out languageId))
            {
                var language = db.Languages.FirstOrDefault(lang => lang.Id == languageId);
                if (language != null)
                {
                    currMovie.LanguageId = languageId;
                }
            }

            int[] selectedCountriesIndeces = this.ListBoxNewMovieCountries.GetSelectedIndices();
            if (selectedCountriesIndeces.Length > 0)
            {
                var     allCountries        = this.ListBoxNewMovieCountries.Items;
                string  currSelectedCountry = string.Empty;
                Country currCountry;
                currMovie.Countries.Clear();
                foreach (int country in selectedCountriesIndeces)
                {
                    currSelectedCountry = allCountries[country].Value;
                    currCountry         = db.Countries.FirstOrDefault(c => c.Id.ToString() == currSelectedCountry);

                    if (currCountry == null)
                    {
                        this.ValidationSummaryMessages.Text = "Invalid Country entered!";
                        return;
                    }

                    currMovie.Countries.Add(currCountry);
                }
            }

            int[] selectedGenresIndeces = this.ListBoxNewMovieGenres.GetSelectedIndices();
            if (selectedGenresIndeces.Length > 0)
            {
                var    allGenres         = this.ListBoxNewMovieGenres.Items;
                string currSelectedGener = string.Empty;
                Genre  currGenre;
                currMovie.Genres.Clear();
                foreach (int gener in selectedGenresIndeces)
                {
                    currSelectedGener = allGenres[gener].Value;
                    currGenre         = db.Genres.FirstOrDefault(g => g.Id.ToString() == currSelectedGener);

                    if (currGenre == null)
                    {
                        this.ValidationSummaryMessages.Text = "Invalid Genre entered!";
                        return;
                    }

                    currMovie.Genres.Add(currGenre);
                }
            }

            if (FileUploadNewMoviePoster.HasFile)
            {
                try
                {
                    string contentType = FileUploadNewMoviePoster.PostedFile.ContentType;
                    if (contentType == "image/jpeg" || contentType == "image/jpg" ||
                        contentType == "image/png" || contentType == "image/gif")
                    {
                        string filename = Path.GetFileName(FileUploadNewMoviePoster.FileName);
                        FileUploadNewMoviePoster.SaveAs(Server.MapPath("~/Images/") + filename);
                    }
                    else
                    {
                        this.ValidationSummaryMessages.Text = "Upload status: Only Image files are accepted!";
                        return;
                    }
                }
                catch (Exception ex)
                {
                    this.ValidationSummaryMessages.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                    return;
                }

                currMovie.PosterPath = "~/Images/" + this.FileUploadNewMoviePoster.FileName;
            }

            db.Entry <Movie>(currMovie).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

            Response.Redirect("~/MovieDetails.aspx?id=" + currMovie.Id);
        }