public async Task <IActionResult> Create([Bind("Id,Num,Date,Sender,Recipient")] InvoiceNum invoiceNum, string[] markSparepartsInInvoice) { if (ModelState.IsValid) { _context.Add(invoiceNum); await _context.SaveChangesAsync(); foreach (var item in markSparepartsInInvoice) { int idSparePart; string numSpareParts; Char delimiter = '|'; String[] substrings = item.Split(delimiter); idSparePart = Int32.Parse(substrings[0]); numSpareParts = substrings[1]; InvoiceSparepartNum invoiceSparepartNum = new InvoiceSparepartNum { InvoiceNumId = _context.InvoiceNums.LastOrDefault().Id, SparepartId = idSparePart, Num = numSpareParts }; _context.Add(invoiceSparepartNum); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(invoiceNum)); }
public String ToString() { try { return(InvoiceNum.ToString()); } catch (Exception ex) { throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message); } }
public async Task <IActionResult> Edit(int id, [Bind("Id,Num,Date,Sender,Recipient")] InvoiceNum invoiceNum, string[] markSparepartsInInvoice) { if (id != invoiceNum.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(invoiceNum); await _context.SaveChangesAsync(); List <InvoiceSparepartNum> listForDell = _context.InvoiceSparepartNums.Where(i => i.InvoiceNumId == id).ToList(); _context.InvoiceSparepartNums.RemoveRange(listForDell); foreach (var item in markSparepartsInInvoice) { int idSparePart; string numSpareParts; Char delimiter = '|'; String[] substrings = item.Split(delimiter); idSparePart = Int32.Parse(substrings[0]); numSpareParts = substrings[1]; InvoiceSparepartNum invoiceSparepartNum = new InvoiceSparepartNum { InvoiceNumId = _context.InvoiceNums.LastOrDefault().Id, SparepartId = idSparePart, Num = numSpareParts }; _context.Add(invoiceSparepartNum); } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!InvoiceNumExists(invoiceNum.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(invoiceNum)); }