public async Task <IActionResult> Create([Bind("Id,Slug,Title,Text,Price,Currency,Quantity,AdvertisementsCategories,AdvertisementsAttributes,ImageId,GalleryId,DateTime,UserId")] Advertisement advertisement, int[] AdvertisementsCategories, int[] AdvertisementsAttributes) { if (ModelState.IsValid) { advertisement.DateTime = DateTime.Now; _context.Add(advertisement); await _context.SaveChangesAsync(); //Categories foreach (var item in AdvertisementsCategories) { AdvertisementCategory advertisementCategory = new AdvertisementCategory() { AdvertisementId = advertisement.Id, CategoryId = item }; _context.AdvertisementsCategories.Add(advertisementCategory); } //Attributes foreach (var item in AdvertisementsAttributes) { AdvertisementAttribute advertisementAttribute = new AdvertisementAttribute() { AdvertisementId = advertisement.Id, AttributeId = item }; _context.AdvertisementsAttributes.Add(advertisementAttribute); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["Users"] = new SelectList(_context.Users, "Id", "UserName", advertisement.UserId); return(View(advertisement)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Slug,Title,Text,Price,Currency,Quantity,AdvertisementsCategories,AdvertisementsAttributes,ImageId,GalleryId,DateTime,UserId")] Advertisement advertisement, int[] AdvertisementsCategories, int[] AdvertisementsAttributes) { if (id != advertisement.Id) { return(NotFound()); } if (ModelState.IsValid) { try { if (advertisement.DateTime == null) { advertisement.DateTime = DateTime.Now; } _context.Update(advertisement); await _context.SaveChangesAsync(); //Categories var advertisementCategoryList = await _context.AdvertisementsCategories.Where(p => p.AdvertisementId == id).ToListAsync(); foreach (var item in advertisementCategoryList) { var advertisementCategory = await _context.AdvertisementsCategories.FirstOrDefaultAsync(p => p.AdvertisementId == id); _context.AdvertisementsCategories.Remove(advertisementCategory); await _context.SaveChangesAsync(); } foreach (var item in AdvertisementsCategories) { AdvertisementCategory advertisementCategory = new AdvertisementCategory() { AdvertisementId = advertisement.Id, CategoryId = item }; _context.AdvertisementsCategories.Add(advertisementCategory); } //Attributes var advertisementAttributeList = await _context.AdvertisementsAttributes.Where(p => p.AdvertisementId == id).ToListAsync(); foreach (var item in advertisementAttributeList) { var advertisementAttribute = await _context.AdvertisementsAttributes.FirstOrDefaultAsync(p => p.AdvertisementId == id); _context.AdvertisementsAttributes.Remove(advertisementAttribute); await _context.SaveChangesAsync(); } foreach (var item in AdvertisementsAttributes) { AdvertisementAttribute advertisementAttribute = new AdvertisementAttribute() { AdvertisementId = advertisement.Id, AttributeId = item }; _context.AdvertisementsAttributes.Add(advertisementAttribute); } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AdvertisementExists(advertisement.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["AdvertisementsAttributes"] = await _context.AdvertisementsAttributes.Where(a => a.AdvertisementId == id).ToListAsync(); ViewData["AdvertisementsCategories"] = await _context.AdvertisementsCategories.Where(a => a.AdvertisementId == id).ToListAsync(); ViewData["Users"] = new SelectList(_context.Users, "Id", "UserName", advertisement.UserId); return(View(advertisement)); }