public ActionResult TheCategory(int id) { CategoryBooks categoryBooks = new CategoryBooks(); categoryBooks.Books = db.Books.Include(a => a.AUTHOR).Include(c => c.CATEGORY).Where(x => x.CATEGORY.CATEGORY_ID == id).ToList(); categoryBooks.Category = db.Categories.Find(id); return(View(categoryBooks)); }
public ActionResult Create([Bind(Include = "Name,Page,Description,Price,Title,Photo,PdfLink")] Book book, int[] checkAuthor, int[] selectCategories) { if (!ModelState.IsValid) { return(View()); } if (checkAuthor.Length > 0) { foreach (var item in checkAuthor) { book.Authors.Add(_context.Authors.Find(item)); } } if (selectCategories.Length > 0) { foreach (var item in selectCategories) { var category = new CategoryBooks() { CategoryId = item, BookId = book.Id }; _context.CategoryBooks.Add(category); } } if (book.Photo == null) { return(View(book)); } if (!book.Photo.IsImage()) { ModelState.AddModelError("Photo", "Choose Photo is valid"); return(View(book)); } book.Image = book.Photo.SaveFiles("Images"); if (book.PdfLink == null) { ModelState.AddModelError("PdfLink", "Please choose is file "); return(View(book)); } if (!book.PdfLink.IsPdf()) { ModelState.AddModelError("PdfLink", "Choose file is valid"); return(View(book)); } book.Pdf = book.PdfLink.SaveFiles("Pdf"); book.Date = DateTime.Now; _context.Books.Add(book); _context.SaveChanges(); return(RedirectToAction("About")); }
public ActionResult Create([Bind(Include = "BookID,title,premiereDate,PublisherID,AuthorID,CategoryID,description,state,ISBN,LendID")] Book book) { if (ModelState.IsValid) { db.Books.Add(book); CategoryBooks CategoryBook = new CategoryBooks(); CategoryBook.BookID = book.BookID; CategoryBook.CategoryID = book.CategoryID; db.CategoryBooks.Add(CategoryBook); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "name", book.CategoryID); return(View(book)); }
public async Task <int?> CreateAsync( int authorId, string title, string description, decimal price, int copies, int edition, int?ageRestriction, DateTime releaseDate, IEnumerable <int> categoryIds) { var book = new Book { AuthorId = authorId, Title = title.Capitalize(), Description = description.Capitalize(), Price = price, Copies = copies, Edition = edition, AgeRestriction = ageRestriction, ReleaseDate = releaseDate }; var categoriesInBook = new List <CategoryBooks>(); foreach (var categoryId in categoryIds) { var categoryBooks = new CategoryBooks { BookId = book.Id, CategoryId = categoryId }; categoriesInBook.Add(categoryBooks); } book.Categories.AddRange(categoriesInBook); this.db.Add(book); await this.db.SaveChangesAsync(); return(book.Id); }
public ActionResult Edit([Bind(Include = "Name,Page,Description,Price, Title,Photo,PdfLink,Id")] Book book, int[] category, int[] authors) { if (!ModelState.IsValid) { return(View(book)); } var bookdb = _context.Books.Where(p => p.Id == book.Id).First(); bookdb.Authors.Clear(); if (authors != null) { foreach (var item in authors) { bookdb.Authors.Add(_context.Authors.Find(item)); } } if (category != null) { foreach (var item in category) { var categorydb = new CategoryBooks() { CategoryId = item, BookId = book.Id }; _context.CategoryBooks.Remove(categorydb); } } if (book.Photo != null) { if (!book.Photo.IsImage()) { ModelState.AddModelError("Photo", "Photo type is invalid"); book.Image = bookdb.Image; return(View(book)); } RemoveFile(bookdb.Image); bookdb.Image = book.Photo.SaveFiles("Images"); } if (book.PdfLink != null) { if (!book.PdfLink.IsPdf()) { ModelState.AddModelError("Photo", "Photo type is invalid"); book.Pdf = bookdb.Pdf; return(View(book)); } RemoveFile(bookdb.Pdf); bookdb.Pdf = book.PdfLink.SaveFiles("Pdf"); } bookdb.Date = DateTime.Now; bookdb.Title = book.Title; bookdb.Description = book.Description; bookdb.Name = book.Name; bookdb.Price = book.Price; bookdb.Page = book.Page; _context.SaveChanges(); return(RedirectToAction("About")); }