public async Task <IActionResult> PutProductCart(int id, ProductCart productCart) { if (id != productCart.Id) { return(BadRequest()); } _context.Entry(productCart).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductCartExistsById(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> AboutEdit(About about) { if (!ModelState.IsValid) { return(NotFound()); } var aboutFromDb = await _db.Abouts.FirstOrDefaultAsync(); if (about.Photo != null) { if (!about.Photo.IsPhoto()) { ModelState.AddModelError("Photo", "File must be image type."); return(View(aboutFromDb)); } Delete(_env.WebRootPath + aboutFromDb.Image); aboutFromDb.Image = await about.Photo.SavePhotoAsync(_env.WebRootPath, "about"); } aboutFromDb.Title = about.Title; aboutFromDb.Description = about.Description; _db.Entry(aboutFromDb).State = EntityState.Modified; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(About))); }
public string EditBook(Book book) { db.Entry(book).State = EntityState.Modified; db.SaveChanges(); //return RedirectToAction("home\AllBooks"); return("Книга изменена " + book.Name + " , " + book.Price + " , " + book.AuthorId + ", Поздравляем!"); }
public bool DeleteBook(Book book) { /* _context.Books.Remove(book); * _context.SaveChanges(); * return true; */ _context.Entry(book).State = EntityState.Deleted; return(_context.SaveChanges() > 0); }
public virtual void Delete(TEntity entityToDelete) { if (context.Entry(entityToDelete).State == EntityState.Detached) { dbSet.Attach(entityToDelete); } dbSet.Remove(entityToDelete); }
public ActionResult Edit([Bind(Include = "BookId,Name,Year,ImagePath,ContentPath,Description,Rating,language,CreateTime")] Book book, int[] selectedAuthors, int[] selectedCategories, HttpPostedFileBase contentFile, HttpPostedFileBase imageFile) { if (ModelState.IsValid && contentFile != null && imageFile != null) { string imageFileName = Path.GetFileName(imageFile.FileName); string imageExtension = Path.GetExtension(imageFile.FileName); List <string> imageExtensions = new List <string>() { ".jpg", ".png" }; string contentFileName = Path.GetFileName(contentFile.FileName); string contentExtension = Path.GetExtension(contentFile.FileName); List <string> contentExtensions = new List <string>() { ".pdf", ".txt", ".xml" }; if (imageExtensions.Contains(imageExtension) && contentExtensions.Contains(contentExtension)) { imageFile.SaveAs(Server.MapPath("/Content/BookImage/" + imageFileName)); contentFile.SaveAs(Server.MapPath("/Content/Books/" + contentFileName)); ViewBag.Message = "Файл сохранен"; } else { ViewBag.Message = "Ошибка расширения файлов "; } if (selectedAuthors != null) { foreach (var c in db.Authors.Where(co => selectedAuthors.Contains(co.AuthorId))) { book.Authors.Add(c); } } if (selectedCategories != null) { foreach (var c in db.Categories.Where(co => selectedCategories.Contains(co.CategoryId))) { book.Categories.Add(c); } } book.CreateTime = DateTime.Now; book.ImagePath = "/Content/BookImage/" + imageFileName; book.ContentPath = "/Content/Books/" + contentFileName; db.Entry(book).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } else if (ModelState.IsValid && contentFile == null && imageFile == null) { db.Entry(book).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(book)); }
public int Save(Book book) { if (book.BookId.Equals(0)) { _context.Books.Add(book); } else { _context.Entry(book).State = EntityState.Modified; } return(_context.SaveChanges()); }
public async Task <IActionResult> Put(string id, [FromBody] Book updatedBook) { updatedBook.ISBN = id; _context.Entry(updatedBook).State = EntityState.Modified; _context.Entry(updatedBook).Property(x => x.Price).IsModified = false; try { await _context.SaveChangesAsync(); } catch { return(NotFound("")); } return(NoContent()); //204 }
public List <Book> GetBooksAll() { var qry = _context.Books.OrderBy(p => p.BookName); foreach (Book a in qry) { _context.Entry(a).Navigation("Author").Load(); } PagingList <Book> BooksList; BooksList = PagingList.Create(qry, _context.Books.Count() + 1, 1); return(BooksList.ToList()); }
public async Task <IActionResult> PutBook(int id, Book book) { if (id != book.ID) { return(BadRequest()); } if (book.CoverPhoto != null) { book.GUID = ImageUpload.WriteFile(book.FileName, book.CoverPhoto); //save new image to static folder book.CoverPhoto = null; book.FilePath = _config["ImagePath"] + book.GUID; } _DBContext.Entry(book).State = EntityState.Modified; try { await _DBContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public bool Edit(Book book) { Boolean res = false; using (var db = new BookContext()) { db.Entry(book).State = System.Data.EntityState.Modified; db.SaveChanges(); res = true; } return res; }