public IActionResult UpdateCampaignByCat(CampaignByCatCommand campaignCommand) { try { var result = _context.Campaigns.FirstOrDefault(x => x.Id == campaignCommand.Id); if (result is null) { ErrorMessage.Message = "Not Found Item"; return(BadRequest(ErrorMessage)); } else if (campaignCommand.ListCampaignCategory.Count == 0) { ErrorMessage.Message = "Please input ProductList"; return(BadRequest(ErrorMessage)); } var campaign = result; campaign.Id = campaignCommand.Id; campaign.PromotionName = campaignCommand.PromotionName; campaign.PromotionDetail = campaignCommand.PromotionDetail; campaign.StartDate = campaignCommand.StartDate; campaign.EndDate = campaignCommand.EndDate; campaign.UpdateDate = DateTime.Now; campaign.PromotionType = campaignCommand.PromotionType; campaign.Discount = campaignCommand.Discount; campaign.ShopId = campaignCommand.ShopId; campaign.IsDelete = false; _context.Entry(campaign).State = EntityState.Modified; _context.SaveChangesAsync(); List <CampaignDetailByCategory> campaignDetailByCat = campaignCommand.ListCampaignCategory.Select(x => new CampaignDetailByCategory { CategoryId = x.CategoryId, UpdateDate = DateTime.Now, CampaignsId = campaign.Id, IsDelete = x.IsDelete }).ToList(); foreach (var item in campaignDetailByCat) { _context.Entry(item).State = EntityState.Modified; } _context.SaveChangesAsync(); SucessMeassage.Message = "Sucess"; return(Ok(SucessMeassage)); } catch (Exception ex) { ErrorMessage.Message = ex.Message; return(StatusCode((int)HttpStatusCode.InternalServerError, ErrorMessage)); } }
public ActionResult Edit([Bind(Include = "CampaignID,CampaignName,ProductID,StartDate,EndDate")] Campaign campaign) { ViewData["ProductList"] = ProductList(); if (ModelState.IsValid) { db.Entry(campaign).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(campaign)); }