public ActionResult AddLineItem([Bind(Include = "SalesInvoiceId,Quantity,ProductId")] SalesInvoiceLineItemViewModel salesInvoiceLineItemViewModel) { var salesInvoice = _salesInvoiceService.Get(t => t.SalesInvoiceId == salesInvoiceLineItemViewModel.SalesInvoiceId, null, "SalesLineItems").FirstOrDefault(); int exists = salesInvoice.SalesLineItems.Where(t => t.ProductId == salesInvoiceLineItemViewModel.ProductId).ToList().Count; bool available = _salesInvoiceService.IsAvailable(salesInvoiceLineItemViewModel.ProductId, salesInvoice.BranchId, salesInvoiceLineItemViewModel.Quantity); if (exists > 0 || !available) { return(RedirectToAction("Create", "SalesInvoices", new { id = salesInvoice.SalesInvoiceId })); } var salesInvoiceLineItem = new SalesLineItem() { ProductId = salesInvoiceLineItemViewModel.ProductId, Quantity = salesInvoiceLineItemViewModel.Quantity, SalesInvoiceId = salesInvoiceLineItemViewModel.SalesInvoiceId, UnitPrice = _productService.FindBy(s => s.ProductcId == salesInvoiceLineItemViewModel.ProductId).First().UnitPrice }; salesInvoice.SalesLineItems.Add(salesInvoiceLineItem); //_salesInvoiceService.EditSalesInvoice(salesInvoice); _salesInvoiceService.AddInvoiceLineItem(salesInvoice, salesInvoiceLineItem); return(RedirectToAction("Create", "SalesInvoices", new { id = salesInvoice.SalesInvoiceId })); }