// GET: Books public ActionResult Index(string searchString, int page = 1) { try { var books = db.Books.AsQueryable(); int pageSize = 10; if (!string.IsNullOrEmpty(searchString)) { books = (db.Books.Include(b => b.Author).Include(b => b.CountryPublished).Where(n => n.Title.Contains(searchString) || n.Author.FullName.Contains(searchString))); if (!books.Any()) { return(PartialView("SearchViewNotFound", searchString)); } } BooksListModel model = new BooksListModel { BooksList = BookRelase.GetBookResult(books) }; if (page > model.BooksList.ToPagedList(page, pageSize).PageCount) { return(RedirectToAction("Index")); } return(Request.IsAjaxRequest() ? (ActionResult)PartialView("IndexPartial", model.BooksList.ToPagedList(page, pageSize)) : View(model.BooksList.ToPagedList(page, pageSize))); } catch { return(RedirectToAction("Index")); } }
// GET: Books/Details/5 public async Task <ActionResult> Details(int?id) { BookViewModel model; try { Book book = await db.Books.FindAsync(id); if (id == null) { //return View(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); return(PartialView("ViewPartial", id.ToString())); } if (book == null) { //return HttpNotFound(); return(PartialView("ViewPartial", id.ToString())); } model = BookRelase.DetailsBook(book); return(View(model)); } catch { return(RedirectToAction("Index")); } }
public async Task <ActionResult> Details(int?id) { Book book; BookViewModel model; try { if (id == null) { //return new HttpStatusCodeResult(HttpStatusCode.BadRequest); return(PartialView("IndexNotFound", id.ToString())); } book = await db.Books.FindAsync(id); if (book == null) { //return HttpNotFound(); return(PartialView("IndexNotFound", id.ToString())); } model = BookRelase.DetailsBook(book); } catch { return(Content("Error")); } return(View(model)); }
//[ValidateAntiForgeryToken] public async Task <ActionResult> Edit(int?id) { try { if (id == null) { //return new HttpStatusCodeResult(HttpStatusCode.BadRequest); return(PartialView("IndexNotFound", id.ToString())); } Book book = await db.Books.FindAsync(id); if (book == null) { //return HttpNotFound(); return(PartialView("IndexNotFound", id.ToString())); } var modelBook = BookRelase.EditBook(book); ViewBag.AuthorsId = new SelectList(db.Authors, "Id", "FullName", book.AuthorsId); ViewBag.CountryPublishedId = new SelectList(db.CountryPublisheds, "Id", "CountryName", book.CountryPublishedId); return(View(modelBook)); } catch { return(RedirectToAction("Index")); } }
public async Task <ActionResult> Create([Bind(Include = "Id,Title,Price,Description,PagesCount,Picture,ImagePatchs,CountryPublishedId,AuthorsId")] BookViewModel model, HttpPostedFileBase upload) { try { if (ModelState.IsValid) { if (upload != null) { var supportedTypes = new[] { "jpg", "jpeg", "png" }; var fileExt = System.IO.Path.GetExtension(upload.FileName).Substring(1); if (!supportedTypes.Contains(fileExt)) { return(RedirectToAction("Index")); //ModelState.AddModelError("ImagePatchs", "Invalid type. Only the following types (jpg, jpeg, png) are supported."); } string filename = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName); string patch = Path.Combine(Server.MapPath("~/Images"), filename); //upload.SaveAs(patch); WebImage img = new WebImage(upload.InputStream); if (img.Width > 270) { img.Resize(260, 400); } img.Save(patch); model.ImagePatchs = new List <ImagePatch>() { new ImagePatch { ImageUrl = filename } }; } else { model.ImagePatchs = new List <ImagePatch>() { new ImagePatch { ImageUrl = "No.jpg" } }; } var v = BookRelase.CreateBook(model); db.Books.Add(v); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewBag.AuthorsId = new SelectList(db.Authors, "Id", "FullName", model.AuthorsId); ViewBag.CountryPublishedId = new SelectList(db.CountryPublisheds, "Id", "CountryName", model.CountryPublishedId); return(View(model)); } catch { return(RedirectToAction("Index")); } }
// GET: Admin public ActionResult Index(string searchString, string sortOption, int page = 1) { try { int pageSize = 5; var books = db.Books.ToList(); if (!string.IsNullOrEmpty(searchString)) { books = (db.Books.Include(b => b.Author).Include(b => b.CountryPublished).Where(n => n.Title.Contains(searchString) || n.Author.FullName.Contains(searchString))).ToList(); if (!books.Any()) { //return Content("This book is not found <a href='~Admin/Index'>Go</a> "); return(PartialView("BookNot", searchString)); } } BooksListModel model = new BooksListModel { BooksList = BookRelase.GetBookResult(books) }; switch (sortOption) { case "Title_ASC": model.BooksList = model.BooksList.OrderBy(n => n.Title).ToList(); break; case "Title_DESC": model.BooksList = model.BooksList.OrderByDescending(n => n.Title).ToList(); break; case "Price_ASC": model.BooksList = model.BooksList.OrderBy(n => n.totalPrice).ToList(); break; case "Price_DESC": model.BooksList = model.BooksList.OrderByDescending(n => n.totalPrice).ToList(); break; case "Author_ASC": model.BooksList = model.BooksList.OrderBy(n => n.Author.FullName).ToList(); break; case "Author_DESC": model.BooksList = model.BooksList.OrderByDescending(n => n.Author.FullName).ToList(); break; case "PageCount_ASC": model.BooksList = model.BooksList.OrderBy(n => n.PagesCount).ToList(); break; case "PageCount_DESC": model.BooksList = model.BooksList.OrderByDescending(n => n.PagesCount).ToList(); break; case "Country_ASC": model.BooksList = model.BooksList.OrderBy(n => n.CountryPublished.CountryName).ToList(); break; case "Country_DESC": model.BooksList = model.BooksList.OrderByDescending(n => n.CountryPublished.CountryName).ToList(); break; default: model.BooksList = model.BooksList.OrderBy(n => n.Id).ToList(); break; } if (page > model.BooksList.ToPagedList(page, pageSize).PageCount) { return(RedirectToAction("Index")); } return(Request.IsAjaxRequest() ? (ActionResult)PartialView("IndexPartial", model.BooksList.ToPagedList(page, pageSize)) : View(model.BooksList.ToPagedList(page, pageSize))); } catch { return(RedirectToAction("Index")); } }
public ActionResult Index() { var books = db.Books.ToList(); BooksListModel model = new BooksListModel { BooksList = BookRelase.GetBookResult(books) }; Random rnd = new Random(); return(View(model.BooksList.OrderBy(n => rnd.Next()).Take(5))); }
public async Task <ActionResult> Edit([Bind(Include = "Id,Title,Price,Description,PagesCount,Picture,ImagePatchs,CountryPublishedId,AuthorsId")] BookViewModel model, HttpPostedFileBase upload) { try { if (ModelState.IsValid) { //book = await db.Books.FindAsync(book.Id); ImagePatch oldpatch = db.ImagePatchs.Where(n => n.BooksId == model.Id).FirstOrDefault(); string pat = oldpatch.ImageUrl; if (upload != null) { var supportedTypes = new[] { "jpg", "jpeg", "png" }; var fileExt = System.IO.Path.GetExtension(upload.FileName).Substring(1); if (!supportedTypes.Contains(fileExt)) { return(RedirectToAction("Index")); // ModelState.AddModelError("photo", "Invalid type. Only the following types (jpg, jpeg, png) are supported."); } string filename = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName); string patch = Path.Combine(Server.MapPath("~/Images"), filename); upload.SaveAs(patch); ImagePatch patchimg = db.ImagePatchs.Where(n => n.BooksId == model.Id).FirstOrDefault(); if (patchimg != null) { patchimg.ImageUrl = filename; string patch1 = Path.Combine(Server.MapPath("~/Images"), pat); if (System.IO.File.Exists(patch1)) { System.IO.File.Delete(patch1); } } } Book b = BookRelase.EditBook(model); db.Entry(b).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewBag.AuthorsId = new SelectList(db.Authors, "Id", "FullName", model.AuthorsId); ViewBag.CountryPublishedId = new SelectList(db.CountryPublisheds, "Id", "CountryName", model.CountryPublishedId); return(View(model)); } catch { return(RedirectToAction("Index")); } }