示例#1
0
        public async Task <IActionResult> Create(AddInstrumentViewModel vm)
        {
            int quantity = vm.Quantity;

            if (ModelState.IsValid)
            {
                for (int i = 0; i < quantity; i++)
                {
                    InstrumentInventory instrumentInventory = new InstrumentInventory();
                    instrumentInventory.InstrumentId = vm.InstrumentId;
                    instrumentInventory.Manufacturer = vm.Manufacturer;
                    instrumentInventory.PurchaseDate = DateTime.Now;
                    instrumentInventory.Cost         = vm.Cost;
                    _context.Add(instrumentInventory);
                }

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            foreach (var error in ViewData.ModelState.Values.SelectMany(modelState => modelState.Errors))
            {
                var test = error;
            }



            ViewData["InstrumentId"] = new SelectList(_context.Instrument, "InstrumentId", "Instrument1", vm.InstrumentId);
            return(View(vm));
        }
示例#2
0
        public async Task <IActionResult> Edit(int id, [Bind("InstrumentInventoryId,InstrumentId,PurchaseDate,Manufacturer,Cost")] InstrumentInventory instrumentInventory)
        {
            if (id != instrumentInventory.InstrumentInventoryId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(instrumentInventory);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!InstrumentInventoryExists(instrumentInventory.InstrumentInventoryId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["InstrumentId"] = new SelectList(_context.Instrument, "InstrumentId", "Instrument1", instrumentInventory.InstrumentId);
            return(View(instrumentInventory));
        }