Esempio n. 1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,RestaurantId,CategoryId,Name,Description,Price,Images,Status,CreatedAt,UpdatedAt")] Food food, IFormFile photo)
        {
            if (id != food.Id)
            {
                return(NotFound());
            }
            if (photo != null)
            {
                var fileName = System.Guid.NewGuid().ToString().Replace("-", "");
                var ext      = photo.ContentType.Split(new char[] { '/' })[1];
                var path     = Path.Combine(webHostEnvironment.WebRootPath, "Foods", fileName + "." + ext);
                await using (var file = new FileStream(path, FileMode.Create))
                {
                    await photo.CopyToAsync(file);
                }
                food.Images = fileName + "." + ext;
            }

            if (!ModelState.IsValid)
            {
                return(View("edit", food));
            }
            try
            {
                food.UpdatedAt = DateTime.Now;
                await iFoodService.Update(food);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!iFoodService.FoodExists(food.Id))
                {
                    return(NotFound());
                }
                throw;
            }
            return(RedirectToAction(nameof(Index)));
        }