public ActionResult Save(MovieInputViewModel input) { using (var db = new TheMovieContext()) { try { bool isNew = input.TheMovie.MovieId == 0; input.SelectedGenres = input.SelectedGenres ?? new List <int>(); input.GenreSelections = db.Genres.AsNoTracking().OrderBy(x => x.GenreName).ToList(); var movie = !isNew?db.Movies.Find(input.TheMovie.MovieId) : input.TheMovie; if (isNew) { db.Movies.Add(movie); } else { db.Entry(movie).Property(x => x.Version).OriginalValue = input.TheMovie.Version; db.Entry(movie).State = EntityState.Modified; } movie.Genres = movie.Genres ?? new List <Genre>(); movie.Genres.Clear(); foreach (int g in input.SelectedGenres) { movie.Genres.Add(db.LoadStub <Genre>(g)); } UpdateModel(movie, "TheMovie", includeProperties: null, excludeProperties: new string[] { "Version" }); db.SaveChanges(); input.TheMovie.Version = movie.Version; ModelState.Remove("TheMovie.MovieId"); // No need to remove TheMovie.Version, ASP.NET MVC is not preserving the ModelState of variables with byte array type. // Hence, with byte array, the HiddenFor will always gets its value from the model, not from the ModelState // ModelState.Remove("TheMovie.Version"); input.MessageToUser = input.MessageToUser + " " + string.Format("Saved. {0}", isNew ? "ID is " + input.TheMovie.MovieId : ""); } catch (DbUpdateConcurrencyException) { ModelState.AddModelError(string.Empty, "The record you attempted to edit was already modified by another user since you last loaded it. Open the latest changes on this record"); } } return(View("Input", input)); }
public ActionResult Save(MovieInputViewModel input) { using (var s = Mapper.GetSessionFactory().OpenSession()) using (var tx = s.BeginTransaction()) { try { bool isNew = input.TheMovie.MovieId == 0; input.SelectedGenres = input.SelectedGenres ?? new List <int>(); input.GenreSelections = s.Query <Genre>().OrderBy(x => x.GenreName).ToList(); input.TheMovie.Genres = new List <Genre>(); foreach (int g in input.SelectedGenres) { input.TheMovie.Genres.Add(s.Load <Genre>(g)); } s.SaveOrUpdate(input.TheMovie); // Primary key(MovieId) is automatically set with SaveOrUpdate, and the row version (Version) field too. /* Use this if you want dynamic update(not all columns are set, only those that are changed), * making NH similar to EF. Note: remove s.SaveOrUpdate, leave tx.Commit only * var m = s.Load<Movie>(input.TheMovie.MovieId); * UpdateModel(m, "TheMovie");*/ tx.Commit(); ModelState.Remove("TheMovie.MovieId"); // No need to remove TheMovie.Version, ASP.NET MVC is not preserving the ModelState of variables with byte array type. // Hence, with byte array, the HiddenFor will always gets its value from the model, not from the ModelState // ModelState.Remove("TheMovie.Version"); input.MessageToUser = string.Format("Saved. {0}", isNew ? "ID is " + input.TheMovie.MovieId : ""); } catch (StaleObjectStateException) { ModelState.AddModelError(string.Empty, "The record you attempted to edit was already modified by another user since you last loaded it. Open the latest changes on this record"); } } return(View("Input", input)); }