public ActionResult Edit(BOOKS R, int[] selectedAuthors, int[] selectedGenres) { BOOKS nr = db.BOOKS.Find(R.BK_ID); nr.BK_NAME = R.BK_NAME; nr.BK_INFO = R.BK_INFO; nr.BK_RATING = R.BK_RATING; nr.BK_DAC = R.BK_DAC; try { db.Entry(nr).State = EntityState.Modified; db.SaveChanges(); var BA = from c in db.BOOKS join d in db.BOOKS_AUTHORS on c.BK_ID equals d.BA_BK where d.BA_BK == nr.BK_ID select d; var BG = from c in db.BOOKS join d in db.BOOKS_GENRES on c.BK_ID equals d.BG_BK where d.BG_BK == nr.BK_ID select d; db.BOOKS_GENRES.RemoveRange(BG); db.BOOKS_AUTHORS.RemoveRange(BA); ViewBag.CmpList = db.DICTIONARY_AGE_CATEGORIES.ToList(); ViewBag.a = (from i in db.AUTHORS where i.BOOKS_AUTHORS.Any(x => x.BA_BK == nr.BK_ID) select i).ToList(); ViewBag.g = (from i in db.GENRES where i.BOOKS_GENRES.Any(x => x.BG_BK == nr.BK_ID) select i).ToList(); ViewBag.Authors = db.AUTHORS.ToList(); ViewBag.Genres = db.GENRES.ToList(); if (selectedAuthors != null) { foreach (var c in db.AUTHORS.Where(co => selectedAuthors.Contains(co.AU_ID))) { BOOKS_AUTHORS my = new BOOKS_AUTHORS(); my.BA_AU = c.AU_ID; my.BA_BK = nr.BK_ID; db.BOOKS_AUTHORS.Add(my); } } if (selectedGenres != null) { foreach (var c in db.GENRES.Where(co => selectedGenres.Contains(co.GN_ID))) { BOOKS_GENRES my = new BOOKS_GENRES(); my.BG_GN = c.GN_ID; my.BG_BK = nr.BK_ID; db.BOOKS_GENRES.Add(my); } } db.SaveChanges(); return(RedirectToAction("BooksRender")); } catch { return(View(R)); } }
public ActionResult CreateB(BOOKS R, int[] selectedAuthors, int[] selectedGenres) { try { ViewBag.CmpList = db.DICTIONARY_AGE_CATEGORIES.ToList(); ViewBag.Authors = db.AUTHORS.ToList(); ViewBag.Genres = db.GENRES.ToList(); ViewBag.a = (from i in db.AUTHORS where i.BOOKS_AUTHORS.Any(x => x.BA_BK == R.BK_ID) select i).ToList(); ViewBag.g = (from i in db.GENRES where i.BOOKS_GENRES.Any(x => x.BG_BK == R.BK_ID) select i).ToList(); if (ModelState.IsValid) { db.BOOKS.Add(R); } db.SaveChanges(); if (selectedAuthors != null) { foreach (var c in db.AUTHORS.Where(co => selectedAuthors.Contains(co.AU_ID))) { BOOKS_AUTHORS my = new BOOKS_AUTHORS(); my.BA_AU = c.AU_ID; my.BA_BK = R.BK_ID; db.BOOKS_AUTHORS.Add(my); } } if (selectedGenres != null) { foreach (var c in db.GENRES.Where(co => selectedGenres.Contains(co.GN_ID))) { BOOKS_GENRES my = new BOOKS_GENRES(); my.BG_GN = c.GN_ID; my.BG_BK = R.BK_ID; db.BOOKS_GENRES.Add(my); } } db.SaveChanges(); return(RedirectToAction("BooksRender")); } catch { return(View(R)); } }