// GET: Invoices/Edit/5 public ActionResult Edit(Guid?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Invoice invoice = db.Invoices.Find(id); if (invoice == null) { return(HttpNotFound()); } var taxes = db.Taxes.Where(x => x.Id != invoice.TaxId).Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Country + " - " + x.TaxValue + "%" }).ToList(); var taxesTip = new SelectListItem() { Value = invoice.TaxId.ToString(), Text = "-- Odaberite porez --" }; taxes.Insert(0, taxesTip); var invoiceForEditViewModel = new InvoiceForEditViewModel { Id = invoice.Id, CreatedByUserId = invoice.CreatedByUserId, InvoiceDate = invoice.InvoiceDate, InvoiceNumber = invoice.InvoiceNumber, InvoicePaymentDate = invoice.InvoicePaymentDate, InvoiceReceiver = invoice.InvoiceReceiver, TaxPercentage = invoice.TaxPercentage, Taxes = new SelectList(taxes, "Value", "Text") }; return(View(invoiceForEditViewModel)); }
public ActionResult Edit([Bind(Include = "Id,InvoiceNumber,InvoiceDate,InvoicePaymentDate,TaxPercentage,InvoiceReceiver,CreatedByUserId,TaxId")] InvoiceForEditViewModel invoice) { if (ModelState.IsValid) { var taxValue = db.Taxes.FirstOrDefault(x => x.Id == invoice.TaxId); var invoiceForEdit = new Invoice { Id = invoice.Id, CreatedByUserId = invoice.CreatedByUserId, InvoiceDate = invoice.InvoiceDate, InvoiceNumber = invoice.InvoiceNumber, InvoicePaymentDate = invoice.InvoicePaymentDate, InvoiceReceiver = invoice.InvoiceReceiver, TaxPercentage = taxValue.TaxValue, TaxId = invoice.TaxId }; db.Entry(invoiceForEdit).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(invoice)); }