public IActionResult Add(AuthorVM model, int[] categoryid) { if (ModelState.IsValid) { Author author = new Author(); author.ID = model.ID; author.Name = model.Name; author.SurName = model.Surname; author.EMail = model.EMail; author.Phone = model.Phone; author.BirthDate = model.BirthDate; _newscontext.Authors.Add(author); _newscontext.SaveChanges(); int authorid = author.ID; model.Categories = _newscontext.Categories.ToList(); for (int i = 0; i < categoryid.Length; i++) { AuthorCategory authorcategory = new AuthorCategory(); authorcategory.CategoryID = categoryid[i]; authorcategory.AuthorID = authorid; _newscontext.AuthorCategories.Add(authorcategory); } _newscontext.SaveChanges(); } else { return(View(GetAuthorForAdd())); } return(Redirect("/Admin/Author/Index/")); }
public AuthorCategory GetAuthorCategory(int id) { AuthorCategory autcat = new AuthorCategory(); List <BookAuthor> alfas = _bookauthor.AllQuery.Where(x => x.BookId == id).ToList(); List <BookCategory> betas = _bookcategory.AllQuery.Where(x => x.BookId == id).ToList(); autcat.authorcount = alfas.Count; autcat.categorycount = betas.Count; List <string> BookAuthor = new List <string>(); List <string> BookCategory = new List <string>(); foreach (var alfa in alfas) { BookAuthor.Add(_authors.AllQuery.Where(x => x.Id == alfa.AuthorId).FirstOrDefault().FullName); } foreach (var beta in betas) { BookCategory.Add(_category.AllQuery.Where(x => x.Id == beta.CategoryId).FirstOrDefault().Title); } autcat.BookAuthor = BookAuthor; autcat.BookCategory = BookCategory; return(autcat); }
public IActionResult Edit(AuthorVM model, int[] categoryid) { Author author = _newscontext.Authors.FirstOrDefault(x => x.ID == model.ID); List <CategoryCheckVM> categoryChecks = new List <CategoryCheckVM>(); if (ModelState.IsValid) { author.Name = model.Name; author.SurName = model.Surname; author.EMail = model.EMail; author.Phone = model.Phone; author.BirthDate = model.BirthDate; _newscontext.SaveChanges(); int authorid = author.ID; model.Categories = _newscontext.Categories.Where(q => q.IsDeleted == false).ToList(); int[] selectedCategories = _newscontext.AuthorCategories.Where(q => q.AuthorID == authorid).Select(q => q.CategoryID).ToArray(); foreach (var item in model.Categories) { CategoryCheckVM categoryCheck = new CategoryCheckVM(); categoryCheck.categoryid = item.ID; foreach (var item2 in selectedCategories) { if (item2 == categoryCheck.categoryid) { categoryCheck.IsChecked = true; break; } else { categoryCheck.IsChecked = false; } } categoryCheck.Name = item.CategoryName; categoryChecks.Add(categoryCheck); } model.categoryCheck = categoryChecks.ToArray(); foreach (var item in categoryid) { if (!selectedCategories.Contains(item)) { AuthorCategory authorCategory = new AuthorCategory(); authorCategory.CategoryID = item; authorCategory.AuthorID = authorid; _newscontext.AuthorCategories.Add(authorCategory); } } _newscontext.SaveChanges(); } else { return(View(GetAuthorForEdit(model.ID))); } return(Redirect("/Admin/Author/Index/")); }