Exemple #1
0
 public ActionResult AddMovie(Movie movie)
 {
     if (ModelState.IsValid)
     {
         using (var context = new MovieReviewsDbContext())
         {
             context.Movies.Add(movie);
             context.SaveChanges();
         }
         return(RedirectToAction("Index"));
     }
     return(View(movie));
 }
Exemple #2
0
 public ActionResult Index([Bind(Exclude = "Id")] AddReviewModel userReviewVM, string movie)
 {
     AuthenticateUser();
     if (ModelState.IsValid)
     {
         UserReview userReview = new UserReview();
         using (var context = new MovieReviewsDbContext())
         {
             try
             {
                 var             currentUser = User.Identity.GetUserId();
                 var             movieInDb   = context.Movies.Where(o => o.MovieName == userReviewVM.MovieName).SingleOrDefault();
                 ApplicationUser user        = context.Users.Where(usr => usr.Id == currentUser).SingleOrDefault();
                 userReview.User   = user;
                 userReview.Movie  = movieInDb;
                 userReview.Review = userReviewVM.Review;
                 ModelState.AddModelError("MovieName", "Wrong movie name");
                 if (userReview.Movie == null)
                 {
                     return(View(userReview));
                 }
                 context.UserReviews.Add(userReview);
                 movieInDb.UserReviews.Add(userReview);
                 context.SaveChanges();
             }
             catch (DbEntityValidationException dbEx)
             {
                 foreach (var validationErrors in dbEx.EntityValidationErrors)
                 {
                     foreach (var validationError in validationErrors.ValidationErrors)
                     {
                         Trace.TraceInformation("Property: {0} Error: {1}",
                                                validationError.PropertyName,
                                                validationError.ErrorMessage);
                     }
                 }
             }
             // return Content("<script>alert('Done!')</script>");
             return(RedirectToAction("Movies", "Home"));
         }
     }
     else
     {
         if (movie != null)
         {
             ViewBag.movieName = movie;
         }
         return(View(userReviewVM));
     }
 }
Exemple #3
0
        public ActionResult EditMovies([Bind(Include = "MovieId,MovieName,MovieYear,PathToImage,Description,Category,Stars,Review,RowVersion")] Movie movie, byte[] rowVersion)
        {
            using (var context = new MovieReviewsDbContext())
            {
                if (ModelState.IsValid)
                {
                    try // Check for optimistic concurrency
                    {
                        context.Entry(movie).State = EntityState.Modified;
                        context.SaveChanges();
                        return(RedirectToAction("Index"));
                    }
                    catch (DbUpdateConcurrencyException ex)      // If there is concurrency
                    {
                        var entry         = ex.Entries.Single(); // get the entry
                        var clientValues  = (Movie)entry.Entity;
                        var databaseEntry = entry.GetDatabaseValues();

                        if (databaseEntry == null)
                        { // deleted
                            ModelState.AddModelError("", "Record has been deleted from another user");
                        }
                        else
                        { // values are changed by other user
                            var databaseValues = (Movie)databaseEntry.ToObject();

                            ModelState.AddModelError("", "Record has been update from another user. Please check the values in the list. Otherwise click 'Save' again and re-write the changes. ");
                            movie.RowVersion = databaseValues.RowVersion; // set the row version
                        }
                        return(View(movie));
                    }
                }
                ModelState.AddModelError("", "Record is not valid.");
                return(View(movie));
            }
        }