Exemple #1
0
        public async Task <IActionResult> Create([Bind("Id,CarModel,ReleaseDate,Description,Photo,Price,Currency,PriceGel,Photoname,CarSpecs")] Car car, IFormCollection collection)
        {
            double exchangerate = CurrencyConvert.ExchangeRate(car.Currency) * car.Price;

            car.PriceGel = (int)exchangerate;

            if (ModelState.IsValid)
            {
                string unique_filename = null;
                if (car.Photo != null)
                {
                    string uploadsFolder = Path.Combine(env.WebRootPath, "images");
                    unique_filename = Guid.NewGuid().ToString() + "_" + car.Photo.FileName;
                    string filePath = Path.Combine(uploadsFolder, unique_filename);
                    car.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
                    car.Photoname = unique_filename;
                }
            }

            if (!string.IsNullOrEmpty(collection["CarSpecs"]))
            {
                string carSpecs = collection["CarSpecs"];
                car.CarSpecs = carSpecs;
            }


            if (ModelState.IsValid)
            {
                _context.Add(car);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(car));
        }
Exemple #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,CarModel,ReleaseDate,Description,Photo,Price,Currency,PriceGel,Photoname")] Car car, IFormCollection collection)
        {
            double exchangerate = CurrencyConvert.ExchangeRate(car.Currency) * car.Price;

            car.PriceGel = (int)exchangerate;

            if (!string.IsNullOrEmpty(collection["CarSpecs"]))
            {
                string carSpecs = collection["CarSpecs"];
                car.CarSpecs = carSpecs;
            }

            if (id != car.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(car);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CarExists(car.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(car));
        }