public JsonResult AjaxDeleteUser(string id) { _dbContext.Entry(new User() { Id = id }).State = EntityState.Deleted; _dbContext.SaveChanges(); return(Json(true)); }
public async Task <IActionResult> CreateBook(CreateBookViewModel createBook, IFormFile image) { if (_dbContext.BooksList.Any(x => x.Code == createBook.Code)) { ModelState.AddModelError(string.Empty, "Mã cuốn sách đã tồn tại trong kho"); } if (!ModelState.IsValid) { ViewBag.Categories = _dbContext.CategoriesList.Select(x => new SelectListItem { Text = x.CategoryName, Value = $"{x.Id}", }).ToList(); return(View(createBook)); } string uniqueFileName = null; if (createBook.Image != null) { string uploadsFolder = Path.Combine(_webHostingEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + createBook.Image.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); createBook.Image.CopyTo(new FileStream(filePath, FileMode.Create)); } await _dbContext.BooksList.AddAsync(new Book { Code = createBook.Code, Author = createBook.Author, CategoryId = createBook.CategoryId, NameOfBook = createBook.NameOfBook, Quantity = createBook.Quantity, YearOfPrint = createBook.YearOfPrint, ImageName = uniqueFileName, }); _dbContext.SaveChanges(); return(RedirectToAction("Books")); }