// GET: Instruments/Create public IActionResult Create() { //new instances of view model and instrument createAndEditAnInstrument vm = new createAndEditAnInstrument(); vm.instrument = new Instrument(); return(View(vm)); }
//mostly the same as create, will probably go back and get the imagefile to prepopulate with the previously created image // GET: Instruments/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } createAndEditAnInstrument editAInstrument = new createAndEditAnInstrument(); var instrument = await _context.Instrument.FindAsync(id); if (instrument == null) { return(NotFound()); } editAInstrument.instrument = instrument; return(View(editAInstrument)); }
public async Task <IActionResult> Create(createAndEditAnInstrument vm) { if (ModelState.IsValid) { //getting image file from view model if it exist and converting it into byte for the database if (vm.ImageFile != null) { using (var memoryStream = new MemoryStream()) { await vm.ImageFile.CopyToAsync(memoryStream); vm.instrument.ProductImage = memoryStream.ToArray(); } } _context.Add(vm.instrument); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(vm.instrument)); }
public async Task <IActionResult> Edit(int id, createAndEditAnInstrument editAInstrument) { if (id != editAInstrument.instrument.Id) { return(NotFound()); } if (ModelState.IsValid) { try { if (editAInstrument.ImageFile != null) { using (var memoryStream = new MemoryStream()) { await editAInstrument.ImageFile.CopyToAsync(memoryStream); editAInstrument.instrument.ProductImage = memoryStream.ToArray(); } } ; _context.Update(editAInstrument.instrument); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!InstrumentExists(editAInstrument.instrument.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(editAInstrument.instrument)); }