예제 #1
0
        // GET: Instruments/Create
        public IActionResult Create()
        {
            //new instances of view  model and instrument
            createAndEditAnInstrument vm = new createAndEditAnInstrument();

            vm.instrument = new Instrument();
            return(View(vm));
        }
예제 #2
0
        //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));
        }
예제 #3
0
        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));
        }
예제 #4
0
        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));
        }