public async Task <IActionResult> Create([Bind("ProductID,PoolQuantity,ProductName")] AdminRewardPoolViewModel model) { Product product = _context.Product.Where(p => p.ProductName.ToLower() == model.ProductName.ToLower()).FirstOrDefault(); if (product == null) { ModelState.AddModelError("", "Unable to Find Product"); } else if (product.ProductStock < model.PoolQuantity) { ModelState.AddModelError("", "Not Enough Stock to Reserve for Reward, Current Stock: " + product.ProductStock); } if (ModelState.IsValid) { product.ProductStock -= model.PoolQuantity; _context.Update(product); RewardPool rewardPool = new RewardPool() { PoolQuantity = model.PoolQuantity, ProductID = product.ProductID, RewardPoolStatus = RewardPoolStatus.Ongoing }; _context.Add(rewardPool); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(RedirectToAction("Create")); }
public IActionResult Create() { AdminRewardPoolViewModel model = new AdminRewardPoolViewModel(); model.Product = new Product(); return(View(model)); }