public async Task <Product> Create(Product product) { _context.Add(product); await _context.SaveChangesAsync(); return(product); }
public async Task <IActionResult> Create(Product product) { if (product is null) { return(BadRequest()); } //model validation var newProduct = new Model.Product { Name = product.Name, Description = product.Description, ImageUrl = product.ImageUrl, Price = product.Price, Supplier = product.Supplier }; _dbContext.Add(newProduct); await _dbContext.SaveChangesAsync(); product.Id = newProduct.Id; return(CreatedAtAction(nameof(Get), new { id = newProduct.Id }, product)); }
public async Task <IActionResult> Create([Bind("CategoryId,Name")] Category category) { if (ModelState.IsValid) { _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(category)); }
public async Task <IActionResult> Create([Bind("Id,Name,Price,Quantity,ShippingPrice")] Products products) { if (ModelState.IsValid) { _context.Add(products); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(products)); }
public async Task <IActionResult> Create([Bind("SowRatesId,SowWeight,TraysPerPack,CostPerTray, ProductsId,DateIn,DateOut")] SowRatesL sowRatesL) { if (ModelState.IsValid) { _context.Add(sowRatesL); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(sowRatesL)); }
public void AddProduct(Product product) { try { productDbContext.Add(product); productDbContext.SaveChanges(true); } catch (Exception ex) { throw new Exception("Cannot update data. " + ex.Message); } }
public async Task <IActionResult> Create([Bind("YieldId,Yield,CostPerTray,ProductsId,DateIn,DateOut")] Yields yields) { if (ModelState.IsValid) { _context.Add(yields); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["ProductsId"] = new SelectList(_context.Products, "Id", "Id", yields.ProductsId); return(View(yields)); }
private void SeedData() { if (!dbContext.Products.Any()) { dbContext.Add(new Db.Product() { Id = 1, Name = "Keyword", Price = 20, Inventory = 100 }); dbContext.Add(new Db.Product() { Id = 2, Name = "Mouse", Price = 5, Inventory = 200 }); dbContext.Add(new Db.Product() { Id = 3, Name = "Monitor", Price = 150, Inventory = 1000 }); dbContext.Add(new Db.Product() { Id = 4, Name = "CPU", Price = 200, Inventory = 2000 }); dbContext.SaveChanges(); } }
private void CreateProducts(ProductsDbContext dbContext) { for (int i = 1; i <= 10; i++) { dbContext.Add(new Product() { Id = i, Name = Guid.NewGuid().ToString(), Inventory = i + 10, Price = (decimal)(i * 3.145) }); } dbContext.SaveChanges(); }
public async void SaveList(List <T> list) { try { foreach (var record in list) { _context.Add(record); } await _context.SaveChangesAsync(); } catch (Exception) { throw; } }
private void SeedData(ProductsDbContext dbContext) { if (!dbContext.Products.Any()) { for (var i = 1000; i < 1025; i++) { dbContext.Add(new Db.Product() { Id = i, Name = Guid.NewGuid().ToString(), Inventory = i * 5, Price = (double)i * 45 }); } dbContext.SaveChanges(); } }
public async Task <IActionResult> Create([Bind("ProductId,Name,Description,CategoryProductId,Manufacturer,Supplier,Price")] Product product) { var max = _context.Products.FirstOrDefault(); if (max == null) { product.ProductId = 1; } else { product.ProductId = _context.Products.Max(item => item.ProductId) + 1; } if (ModelState.IsValid) { _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(product)); }
public async Task <IActionResult> Create([Bind("CategoryProductId,CategoryName")] CategoryProduct categoryProduct) { var max = _context.CategoryProducts.FirstOrDefault(); if (max == null) { categoryProduct.CategoryProductId = 1; } else { categoryProduct.CategoryProductId = _context.CategoryProducts.Max(item => item.CategoryProductId) + 1; } if (ModelState.IsValid) { _context.Add(categoryProduct); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(categoryProduct)); }
public void AddProductAsync(Product product) { _context.Add(product); SaveAndUpdateContext(); }