public async Task <IActionResult> Create([Bind("Id,Num,Date,Sender,Recipient")] Invoice invoice, string[] markSparepartsInInvoice) { if (ModelState.IsValid) { _context.Add(invoice); await _context.SaveChangesAsync(); foreach (var item in markSparepartsInInvoice) { InvoiceSparepart invoiceSparepart = new InvoiceSparepart { InvoiceId = _context.Invoices.LastOrDefault().Id, SparepartId = Int32.Parse(item) }; _context.Add(invoiceSparepart); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(invoice)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Num,Date,Sender,Recipient")] Invoice invoice, string[] markSparepartsInInvoice) { if (id != invoice.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(invoice); List <InvoiceSparepart> listForDell = _context.InvoiceSpareparts.Where(i => i.InvoiceId == id).ToList(); _context.InvoiceSpareparts.RemoveRange(listForDell); await _context.SaveChangesAsync(); foreach (var item in markSparepartsInInvoice) { InvoiceSparepart invoiceSparepart = new InvoiceSparepart { InvoiceId = id, SparepartId = Int32.Parse(item) }; _context.Add(invoiceSparepart); } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!InvoiceExists(invoice.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(invoice)); }