public async Task<ActionResult<string>> Save([FromBody] List<ItemParts> parts) { try { if (ModelState.IsValid) { _context.AddRange(parts); await _context.SaveChangesAsync(); return CreatedAtAction("Result", new { id = parts.FirstOrDefault()?.ItemId, saved = true }); } else { return CreatedAtAction("Result", new { id = 0, saved = false }); } } catch (Exception ex) { return CreatedAtAction("Result", new { id = ex.Message, saved = false }); } }
//[ValidateAntiForgeryToken] public async Task <ActionResult <string> > Save([FromBody] Invoices invoice) { try { if (ModelState.IsValid) //if (invoices != null) { // set ip & user id var exitsBefore = await _context.Invoices.FindAsync(invoice.Id); if (exitsBefore != null) { // update exitsBefore.InvoiceNo = invoice.InvoiceNo; exitsBefore.Notes = invoice.Notes; exitsBefore.NetAmount = invoice.NetAmount; exitsBefore.Discount = invoice.Discount; exitsBefore.TotalAmount = invoice.TotalAmount; exitsBefore.AccountId = invoice.AccountId; exitsBefore.InvoiceTypeId = invoice.InvoiceTypeId; exitsBefore.AddDate = invoice.AddDate; _context.Update(exitsBefore); _context.UpdateRange(invoice.InvoiceDetails); } else // insert { _context.Add(invoice); foreach (var itm in invoice.InvoiceDetails) { itm.InvoiceId = invoice.Id; } _context.AddRange(invoice.InvoiceDetails); } await _context.SaveChangesAsync(); return(CreatedAtAction("Result", new { id = invoice.Id, saved = true })); } else { return(CreatedAtAction("Result", new { id = 0, saved = false })); } } catch (Exception ex) { return(CreatedAtAction("Result", new { id = ex.Message, saved = false })); } }
public async Task <IActionResult> Upload(PhotoModel item) { var uploads = Path.Combine(_environment.WebRootPath, "Public"); List <Photos> Photos = new List <Photos>(); var _files = HttpContext.Request.Form.Files; long stockId = item.Car.ItemId; for (int i = 0; i < _files.Count(); i++) { var file = _files[i]; string fileExtension = Path.GetExtension(file.FileName), fileName = string.Format("{0}{1}", Guid.NewGuid().ToString(), fileExtension); if (file.Length > 0) { using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); // Save it to the server Photos.Add(new Photos { // Save it to Db Active = true, Main = false, Url = fileName, ItemId = stockId }); } } } // Submit saving to Db _context.AddRange(Photos); await _context.SaveChangesAsync(); //return RedirectToAction(nameof(Index), new { id = stockId }); return(Ok(new { count = _files.Count })); }