public ActionResult Add(LaptopViewModel viewmodel, string modelName) { ViewBag.LM = context.Stocks.ToList().Where(x => x.category == "Laptop"); List <Stock> slist = new List <Stock>(context.Stocks.ToList().Where(x => x.category == "Laptop")); List <SelectListItem> li = new List <SelectListItem>(); foreach (var item in slist) { var man = li.Find(x => x.Value == item.manufacturer); if (man == null) { li.Add(new SelectListItem { Text = item.manufacturer, Value = item.manufacturer }); } } viewmodel.modelName = modelName; ViewBag.LL = li; ViewBag.LM = context.Stocks.ToList().Where(x => x.category == "Laptop"); if (ModelState.IsValid) { AssetLogic al = new AssetLogic(); try { Stock stock = context.Stocks.FirstOrDefault(m => m.model.Equals(viewmodel.modelName) && m.manufacturer.Equals(viewmodel.manufacturer) && m.category.Equals("Laptop")); if (stock != null && stock.quantity != 0) { var asset = new Asset { manufacturer = viewmodel.manufacturer, serialNumber = viewmodel.serialNumber, dateadded = viewmodel.dateAdded, warranty = viewmodel.warranty + " Months", costprice = viewmodel.costprice, InvoiceNumber = viewmodel.InvoiceNumber, depreciationcost = al.depreciationCost(viewmodel.dateAdded, viewmodel.costprice) }; var laptop = new Laptop { serialNumber = viewmodel.serialNumber, manufacturer = viewmodel.manufacturer, modelName = viewmodel.modelName, warranty = viewmodel.warranty + " Months", dateAdded = viewmodel.dateAdded, HDD = viewmodel.HDD, OS = viewmodel.OS, RAM = viewmodel.RAM, screenSize = viewmodel.screenSize, InvoiceNumber = viewmodel.InvoiceNumber, processor = viewmodel.processor }; stock.quantity = stock.quantity - 1; repository.Insert(asset, laptop); repository.Save(); context.SaveChanges(); TempData["Success"] = "Asset has been added!"; } else { ViewBag.Message = "Asset model not available in stock. Update your stock."; } } catch (Exception e) { ViewBag.Message = "Asset not added. Error: " + e.Message; } } ModelState.Clear(); return(View(viewmodel)); }