예제 #1
0
        public ActionResult Create(Movie movie, FormCollection MovieForm)
        {
            if (ModelState.IsValid) {

                //how to add tags to this movie object? I am getting user selected tag ids posted back
                //movie.Tags = new List<Tag>();
                foreach (var tag in MovieForm["Tags"].Split(','))
                {
                    var _tag = new Tag { tagID = Convert.ToInt32(tag)};

                    //movie.Tags is null

                    movie.Tags.Add(_tag);
                }

                movieRepository.InsertOrUpdate(movie);
                movieRepository.Save();
                return RedirectToAction("Index");
            } else {
                var m = new M2M.ViewModels.MovieCreateViewModel();

                m.Tags = tagRepository.All.ToList<Tag>();
                return View(m);
            }
        }
예제 #2
0
 public void InsertOrUpdate(Movie movie)
 {
     if (movie.MovieID == default(int)) {
         // New entity
         //to add realtion between this new movie and all existed tags in db we need to set state of
         //each tag to Unchanged otherwise it will insert new tags into db with auto genrated Ids and
         //other colums set to null/empty
         foreach (var tag in movie.Tags)
         {
             context.Entry(tag).State = EntityState.Unchanged;
         }
         context.Movies.Add(movie);
     } else {
         // Existing entity
         context.Entry(movie).State = EntityState.Modified;
     }
 }
예제 #3
0
 public ActionResult Edit(Movie movie)
 {
     if (ModelState.IsValid) {
         movieRepository.InsertOrUpdate(movie);
         movieRepository.Save();
         return RedirectToAction("Index");
     } else {
         return View();
     }
 }