Beispiel #1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Type,Brand,ModelName,ModelNum,Image,ImagePath")] DeviceModelEditViewModel vm)
        {
            if (id != vm.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                DeviceModel dm = _mapper.Map <DeviceModel>(vm);

                string fileName = null;
                if (vm.Image != null)
                {
                    string uploadFolderPath = Path.Combine(_hostingEnvironment.WebRootPath, "img\\devicemodel");
                    fileName = Guid.NewGuid().ToString() + "_" + vm.Image.FileName;
                    string completeFilePath = Path.Combine(uploadFolderPath, fileName);
                    vm.Image.CopyTo(new FileStream(completeFilePath, FileMode.Create));

                    // add final property to new DeviceModel
                    dm.ImagePath = fileName;
                }
                // othewise no image is uploaded, existing filename was passed on during mapping (required hidden input in view)
                try
                {
                    _context.Update(dm);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!DeviceModelExists(dm.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }
Beispiel #2
0
        // GET: DeviceModels/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            // check if id itself is null
            if (id == null)
            {
                return(NotFound());
            }

            // check if the id's related DeviceModel is null
            var dm = await _context.DeviceModels.FindAsync(id);

            if (dm == null)
            {
                return(NotFound());
            }


            // TODO any way to concatonate /img/devicemodel/ here???
            DeviceModelEditViewModel vm = _mapper.Map <DeviceModelEditViewModel>(dm);

            return(View(vm));
        }