public async Task <IActionResult> Edit(Page page) { if (ModelState.IsValid) { //Home page has to have "home" slug regardless of the display name page.Slug = page.Id == 1 ? "home" : page.Title.ToLower().Replace(" ", "-"); var slug = await context.Pages.Where(x => x.Id != page.Id).FirstOrDefaultAsync(x => x.Slug == page.Slug); if (slug != null) { ModelState.AddModelError("", "Karta o podanym tytule już istnieje"); //Page already exists return(View(page)); } context.Update(page); await context.SaveChangesAsync(); TempData["Success"] = "Karta została zedytowana pomyślnie"; //Page has been edited return(RedirectToAction("Edit", new { id = page.Id })); } return(View(page)); }
public async Task <IActionResult> EditAddress(DeliveryAddress address) { AppUser appUser = await userManager.FindByNameAsync(User.Identity.Name); if (ModelState.IsValid) { DeliveryAddress newAddress = await context.DeliveryAddresses.FirstOrDefaultAsync(x => x.UserId == appUser.Id); if (newAddress == null) { address.UserId = appUser.Id; context.Add(address); TempData["Success"] = "Adres został dodany"; //Address added } else { newAddress.City = address.City; newAddress.Street = address.Street; newAddress.ZIPCode = address.ZIPCode; context.Update(newAddress); TempData["Success"] = "Adres został zmieniony"; //Address changed } await context.SaveChangesAsync(); } return(RedirectToAction("Details")); }
public async Task <IActionResult> Edit(int id, Book book) { ViewBag.GenreId = new SelectList(context.Genres.OrderBy(x => x.Sorting), "Id", "Name", book.GenreId); if (ModelState.IsValid) { book.Slug = book.Title.ToLower().Replace(" ", "-"); var slug = await context.Books.Where(x => x.Id != id).FirstOrDefaultAsync(x => x.Slug == book.Slug); if (slug != null) { ModelState.AddModelError("", "Książka o podanej nazwie już istnieje"); //Book already exists return(View(book)); } if (book.ImageUpload != null) { string uploadDir = Path.Combine(webHostEnvironment.WebRootPath, "media/books"); //If there is "no image" photo, don't delete it, because its universal photo for many items if (!string.Equals(book.Image, "noimage.png")) { string oldImagePath = Path.Combine(uploadDir, book.Image); if (System.IO.File.Exists(oldImagePath)) { System.IO.File.Delete(oldImagePath); } } //Create a new, unique name for photo and save it string imageName = Guid.NewGuid().ToString() + "_" + book.ImageUpload.FileName; string filePath = Path.Combine(uploadDir, imageName); FileStream fs = new FileStream(filePath, FileMode.Create); await book.ImageUpload.CopyToAsync(fs); fs.Close(); book.Image = imageName; } context.Update(book); await context.SaveChangesAsync(); TempData["Success"] = "Książka została zedytowana"; //Book has been edited return(RedirectToAction("Index")); } return(View(book)); }
public async Task <IActionResult> Edit(Genre genre) { if (ModelState.IsValid) { genre.Slug = genre.Name.ToLower().Replace(" ", "-"); var slug = await context.Genres.Where(x => x.Id != genre.Id).FirstOrDefaultAsync(x => x.Slug == genre.Slug); if (slug != null) { ModelState.AddModelError("", "Gatunek o podanej nazwie już istnieje"); //Genre already exists return(View(genre)); } context.Update(genre); await context.SaveChangesAsync(); TempData["Success"] = "Gatunek został zedytowany"; //Genre has been edited return(RedirectToAction("Edit", new { id = genre.Id })); } return(View(genre)); }