public async Task <IActionResult> OnPost(decimal totalChange, int productId) { var product = await _context.Products.FirstOrDefaultAsync(x => x.Id == productId); if (product == null) { return(NotFound()); } product.Stock--; await _context.SaveChangesAsync(); totalChange = Math.Round(totalChange, 2); var units = CurrencyHelper.GetUSDCommonCoins(); units.AddRange(CurrencyHelper.GetUSDCommonBills()); var change = CurrencyHelper.CalculateChange(totalChange, units); change.All(c => { c.PluralName = char.ToUpper(c.PluralName[0]) + c.PluralName.Substring(1); return(true); }); var changeOutput = change.Select(x => new { x.PluralName, x.Quantity }); var result = new { Change = changeOutput, Stock = (await _context.Products.FirstOrDefaultAsync(x => x.Id == productId))?.Stock ?? 0 }; return(new JsonResult(result)); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(Product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(Product.Id)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var newProduct = new Product(); if (await TryUpdateModelAsync <Product>( newProduct, "product", s => s.Name, s => s.Price, s => s.Stock)) { _context.Products.Add(Product); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }