public ActionResult Edit(FormCollection collection, AddNewBookViewModel model, HttpPostedFileBase file) { try { myHandler = new BusinessLogicHandler(); book = new Book(); if (ModelState.IsValid) { model.books.BookCategoryID = Convert.ToInt32(collection.GetValue("CategoryName").AttemptedValue); model.books.PublisherID = Convert.ToInt32(collection.GetValue("PublisherName").AttemptedValue); model.books.SupplierID = Convert.ToInt32(collection.GetValue("Name").AttemptedValue); string[] Authors = (string[])collection.GetValue("FullName").RawValue; try { if (file != null) { model.books.CoverImage = file.FileName; file.SaveAs(HttpContext.Server.MapPath("~/Uploads/Books/") + file.FileName); } } catch { } #region Clean Book_Authors myHandler.BookAuthorUpdateDelete(model.books.BookID); #endregion #region Add New Book_Authors BookAuthor bookAuthors = new BookAuthor(); bookAuthors.BookID = model.books.BookID; foreach (var item in Authors) //INSERTING BOOK AUTHORS { bookAuthors.AuthorID = Convert.ToInt32(item); myHandler.InsertBookAuthor(bookAuthors); } #endregion myHandler.UpdateBookProduct(model.books); myHandler.UpdateBook(model.books); } return RedirectToAction("AdminIndex", "Book", null); } catch { return View(); } }
public ActionResult Create(FormCollection collection, HttpPostedFileBase file) { try { myHandler = new BusinessLogicHandler(); book = new Book(); book.BookTitle = collection.GetValue("books.BookTitle").AttemptedValue.ToString(); book.Synopsis = collection.GetValue("books.Synopsis").AttemptedValue.ToString(); book.ISBN = collection.GetValue("books.ISBN").AttemptedValue.ToString(); book.BookCategoryID = Convert.ToInt32(collection.GetValue("CategoryName").AttemptedValue); book.PublisherID = Convert.ToInt32(collection.GetValue("PublisherName").AttemptedValue); book.SupplierID = Convert.ToInt32(collection.GetValue("Name").AttemptedValue); //book.AuthorID = Convert.ToInt32(collection.GetValue("FullName").AttemptedValue); string[] Authors = (string[])collection.GetValue("FullName").RawValue; book.CostPrice = Convert.ToDouble(collection.GetValue("books.CostPrice").AttemptedValue); book.SellingPrice = Convert.ToDouble(collection.GetValue("books.SellingPrice").AttemptedValue); book.DateAdded = DateTime.Now; //TryUpdateModel(book); //CONSIDER REMOVING THIS..! if (ModelState.IsValid) { if (file != null) { file.SaveAs(HttpContext.Server.MapPath("~/Uploads/Books/") + file.FileName); book.CoverImage = file.FileName; } Book ba = new Book(); BookAuthor bookAuthors = new BookAuthor(); ba = myHandler.AddExperimentBook(book); ba.BookTitle = book.BookTitle; ba.Synopsis = book.Synopsis; ba.ISBN = book.ISBN; ba.BookCategoryID = book.BookCategoryID; ba.PublisherID = book.PublisherID; ba.SupplierID = book.SupplierID; ba.CoverImage = book.CoverImage; //myHandler.AddBook(ba); bookAuthors = myHandler.TrialInsertBook(ba); foreach (var item in Authors) //INSERTING BOOK AUTHORS { bookAuthors.AuthorID = Convert.ToInt32(item); myHandler.InsertBookAuthor(bookAuthors); } TempData["AlertMessage"] = "Book Successfully Entered"; } //return RedirectToAction("Index", "Book", book); return RedirectToAction("Create", "Book", null); //REDIRECT TO GET ACTION METHOD #INCASE THEY WANT TO INSERT ANOTHER BOOK :) } catch { AddNewBookViewModel model = new AddNewBookViewModel(); #region Create SupplierHandler supHandler = new SupplierHandler(); /*TEMP LIST*/ //List<Supplier> nameList = new List<Supplier>(); IEnumerable<Supplier> nameList = (IEnumerable<Supplier>)supHandler.GetBookSupplierList(); var disp = from nameAndId in nameList select new { Value = nameAndId.SupplierID, Text = nameAndId.Name }; ViewBag.SupplierList = new SelectList(disp.ToList()); BookCategoryHandler typeHandler = new BookCategoryHandler(); IEnumerable<BookCategory> typeList = (IEnumerable<BookCategory>)typeHandler.GetBookCategoryList(); var dispBC = from name in typeList select new { Value = name.BookCategoryID, Text = name.CategoryName }; ViewBag.BookCategoryList = new SelectList(dispBC.ToList()); AuthorHandler authHandler = new AuthorHandler(); IEnumerable<Author> authList = (IEnumerable<Author>)authHandler.GetAuthorList(); var dispAuth = from nameAndSurname in authList select new { Value = nameAndSurname.AuthorID, Text = nameAndSurname.Name, nameAndSurname.Surname }; ViewBag.authList = new SelectList(dispAuth.ToList()); PublisherHandler publHandler = new PublisherHandler(); IEnumerable<Publisher> pubList = (IEnumerable<Publisher>)publHandler.GetPublisherList(); var dispPublisher = from pubName in pubList select new { Value = pubName.PublisherID, Text = pubName.Name }; ViewBag.pubList = new SelectList(dispPublisher.ToList()); #endregion #region Display List<SelectListItem> bookCategory = new List<SelectListItem>(); //bookCategory.Add(new SelectListItem { Text = "Select Category", Value = "", Selected = true }); foreach (var item in typeList) { bookCategory.Add(new SelectListItem { Text = item.CategoryName, Value = item.BookCategoryID.ToString() }); } model.bookCategories = new List<SelectListItem>(); model.bookCategories = bookCategory; ViewData["bookCategories"] = bookCategory; List<SelectListItem> supplier = new List<SelectListItem>(); //supplier.Add(new SelectListItem { Text = "Select Supplier", Value = "", Selected = true }); foreach (var item in nameList) { supplier.Add(new SelectListItem { Text = item.Name, Value = item.SupplierID.ToString() }); } model.suppliers = new List<SelectListItem>(); model.suppliers = supplier; ViewData["suppliers"] = supplier; List<SelectListItem> author = new List<SelectListItem>(); //author.Add(new SelectListItem { Text = "Select Author", Value = "", Selected = true }); foreach (var item in authList) { author.Add(new SelectListItem { Text = item.Name, Value = item.AuthorID.ToString() }); } model.authors = new List<SelectListItem>(); model.authors = author; ViewData["authors"] = author; List<SelectListItem> publisher = new List<SelectListItem>(); //publisher.Add(new SelectListItem { Text = "Select Publisher", Value = "", Selected = true }); foreach (var item in pubList) { publisher.Add(new SelectListItem { Text = item.Name, Value = item.PublisherID.ToString() }); } model.publishers = new List<SelectListItem>(); model.publishers = publisher; ViewData["publishers"] = publisher; #endregion return View(model); } }