public async Task <IActionResult> Edit(int id, [Bind("Id,OffersType,Image,Title,Description,Validity,StartDate,EndDate,Price,SalePrice,SaleFromDate,SaleToDate,Categories")] AdminOffersViewModel offers) { var row = _context.Offers.Include(x => x.OffersCategories).Where(x => x.Id == id).FirstOrDefault(); if (ModelState.IsValid) { string userId = ""; if (!string.IsNullOrEmpty(Request.Form["User"])) { userId = Request.Form["User"]; } else { userId = _userManager.GetUserId(User); } var user = _context.Users.Find(userId); if (offers.Image != null) { string filename = Guid.NewGuid().ToString().Substring(4) + offers.Image.FileName; UploadFile(offers.Image, filename); row.Image = filename; } else { row.Image = row.Image; } row.Description = offers.Description; row.UserId = !string.IsNullOrEmpty(userId)?userId:null; row.EndDate = offers.EndDate; row.OffersType = offers.OffersType; row.Price = offers.Price; row.SaleFromDate = offers.SaleFromDate; row.SalePrice = offers.SalePrice; row.SaleToDate = offers.SaleToDate; row.StartDate = offers.StartDate; row.Title = offers.Title; row.Validity = offers.Validity; _context.OffersCategories.RemoveRange(row.OffersCategories); string[] catIds = Request.Form["Categories"].ToString().Split(','); var dbcats = _context.VendorCategories.Where(x => catIds.Contains(x.Id.ToString())).ToList(); foreach (var dbcat in dbcats) { _context.OffersCategories.Add(new OffersCategory { VendorCategory = dbcat, Offers = row }); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(offers)); }
public async Task <IActionResult> Create([Bind("Id,OffersType,Image,Title,Description,Validity,StartDate,EndDate,Price,SalePrice,SaleFromDate,SaleToDate,Categories")] AdminOffersViewModel offers) { if (ModelState.IsValid) { string filename = ""; if (offers.Image != null && offers.Image.Length > 0) { filename = Guid.NewGuid().ToString().Substring(4) + offers.Image.FileName; UploadFile(offers.Image, filename); } string userId = ""; if (!string.IsNullOrEmpty(Request.Form["User"])) { userId = Request.Form["User"]; } else { userId = _userManager.GetUserId(User); } var user = _context.Users.Find(userId); var Offer = new Offers { Description = offers.Description, EndDate = offers.EndDate, OffersType = offers.OffersType, Image = filename, Price = offers.Price, SaleFromDate = offers.SaleFromDate, SalePrice = offers.SalePrice, SaleToDate = offers.SaleToDate, StartDate = offers.SaleToDate, Title = offers.Title, Validity = offers.Validity, User = user }; _context.Offers.Add(Offer); string[] catIds = Request.Form["Categories"].ToString().Split(','); var dbcats = _context.VendorCategories.Where(x => catIds.Contains(x.Id.ToString())).ToList(); foreach (var dbcat in dbcats) { _context.OffersCategories.Add(new OffersCategory { VendorCategory = dbcat, Offers = Offer }); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(offers)); }
public IActionResult Edit(int?id) { if (id == null) { return(NotFound()); } var users = _context.Users.AsEnumerable(); var userselect = new SelectList(users, "Id", "FirstName"); var offers = _context.Offers.Include(x => x.OffersCategories).Where(x => x.Id == id).FirstOrDefault(); var vendorcategoies = _context.VendorCategories.AsEnumerable(); var catslist = new SelectList(vendorcategoies, "Id", "Title"); foreach (var item in catslist) { foreach (var itemcategory in offers.OffersCategories) { if (item.Value == itemcategory.VendorCategory.Id.ToString()) { item.Selected = true; } } } foreach (var item in userselect) { if (item.Value == offers.UserId) { item.Selected = true; } } AdminOffersViewModel model = new AdminOffersViewModel { Categories = catslist, Description = offers.Description, EndDate = offers.EndDate, Id = offers.Id, OffersType = offers.OffersType, Price = offers.Price, SaleFromDate = offers.SaleFromDate, SalePrice = offers.SalePrice, SaleToDate = offers.SaleToDate, StartDate = offers.StartDate, Title = offers.Title, Validity = offers.Validity, User = userselect }; if (offers == null) { return(NotFound()); } return(View(model)); }