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); } }
public void InsertOrUpdate(Tag tag) { if (tag.tagID == default(int)) { // New entity context.Tags.Add(tag); } else { // Existing entity context.Entry(tag).State = EntityState.Modified; } }