public async Task <IActionResult> Create([Bind("PartId,ItemId,PartName,Qty,Barcode,AvgCost,LastCost,SalePrice,More,Active,Starred,Deleted,LastPurchasedDate,AddDate,PartId1")] ItemParts itemParts) { if (ModelState.IsValid) { itemParts.AvgCost = itemParts.Qty * itemParts.SalePrice; // totalPrice = UnitPrice * Quantity _context.Add(itemParts); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index), new { id = itemParts.ItemId })); } else { var invalid = ModelState.Where(p => p.Value.ValidationState == ModelValidationState.Invalid).FirstOrDefault(); ViewData["ErrorMessage"] = "Error:!" + string.Join(",", invalid.Value.Errors.Select(i => i.ErrorMessage)); } var model = new ItemPartsViewModel() { ItemParts = await _context.ItemParts.Include(i => i.Item).Where(p => p.ItemId == itemParts.ItemId).ToListAsync(), ItemPart = new ItemParts() { ItemId = (long)itemParts.ItemId }, Car = await _context.Items.Include(p => p.Make).Include(p => p.Model).FirstOrDefaultAsync(i => i.ItemId == itemParts.ItemId) }; return(View("Index.cshtml", model)); }
// GET: ItemParts public async Task <IActionResult> Index(long?id) { var partStoreContext = new ItemPartsViewModel() { ItemParts = await _context.ItemParts.Include(i => i.Item).Where(p => p.ItemId == id).ToListAsync(), ItemPart = new ItemParts() { ItemId = (long)id }, Car = await _context.Items.Include(p => p.Make).Include(p => p.Model).FirstOrDefaultAsync(i => i.ItemId == id) }; ViewData["PartId1"] = new SelectList(_context.Parts, "Id", "Name"); return(View(partStoreContext)); }