Exemplo n.º 1
0
        // GET: Gears/Edit/5
        public async Task <ActionResult> Edit(int id)
        {
            var gearOptions = await _context.GearTypes.Select(g => new SelectListItem()
            {
                Text = g.Label, Value = g.Id.ToString()
            })
                              .ToListAsync();

            var gear = await _context.Gears.FirstOrDefaultAsync(g => g.Id == id);

            var viewModel = new GearFormViewModel()
            {
                Name            = gear.Name,
                Condition       = gear.Condition,
                Description     = gear.Description,
                GearTypeId      = gear.GearTypeId,
                Rating          = gear.Rating,
                GearTypeOptions = gearOptions,
            };

            if (gear.ImagePath != null)
            {
                viewModel.ImageString = gear.ImagePath;
            }

            return(View(viewModel));
        }
Exemplo n.º 2
0
        // GET: Gears/Create
        public async Task <ActionResult> Create()
        {
            var gearOptions = await _context.GearTypes.Select(g => new SelectListItem()
            {
                Text = g.Label, Value = g.Id.ToString()
            })
                              .ToListAsync();

            var viewModel = new GearFormViewModel();

            viewModel.GearTypeOptions = gearOptions;
            viewModel.Rating          = 1;
            return(View(viewModel));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Edit(int id, GearFormViewModel gearFormView)
        {
            try
            {
                var user = await GetCurrentUserAsync();

                var gearData = new Gear()
                {
                    Id                = id,
                    Name              = gearFormView.Name,
                    Condition         = gearFormView.Condition,
                    ApplicationuserId = user.Id,
                    Description       = gearFormView.Description,
                    Rating            = gearFormView.Rating,
                    GearTypeId        = gearFormView.GearTypeId,
                };
                var uploadPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images");
                if (gearFormView.ImagePath != null)
                {
                    var fileName = Guid.NewGuid().ToString() + gearFormView.ImagePath.FileName;
                    gearData.ImagePath = fileName;
                    using (var fileStream = new FileStream(Path.Combine(uploadPath, fileName), FileMode.Create))
                    {
                        await gearFormView.ImagePath.CopyToAsync(fileStream);
                    }
                }
                else if (gearFormView.ImageString != null)
                {
                    gearData.ImagePath = gearFormView.ImageString;
                }

                _context.Gears.Update(gearData);
                await _context.SaveChangesAsync();

                // TODO: Add update logic here

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }