Exemplo n.º 1
0
        public IActionResult EditPOST(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var menuItemVm = menuItemAndSubCListAndCListVm.MenuItem;

            menuItemVm.SubCategoryId = Convert.ToInt32(Request.Form["SubCategoryId"].ToString());

            if (!ModelState.IsValid)
            {
                return(View(menuItemAndSubCListAndCListVm));
            }

            string webRootPath = _webHostEnvironment.WebRootPath;
            var    files       = HttpContext.Request.Form.Files;

            if (files.Count > 0)
            {
                var uniqueNameOfString = Math.Round((DateTime.Now - DateTime.MinValue).TotalMilliseconds).ToString();

                //New image has been uploaded
                var uploads       = Path.Combine(webRootPath, "images");
                var extension_new = Path.GetExtension(files[0].FileName);

                //Delete original file
                var imagePath = Path.Combine(webRootPath, menuItemVm.Image.TrimStart('\\'));
                if (System.IO.File.Exists(imagePath))
                {
                    System.IO.File.Delete(imagePath);
                }

                //upload new file
                using (var filesStream = new FileStream(Path.Combine(uploads, uniqueNameOfString + extension_new), FileMode.Create))
                {
                    files[0].CopyTo(filesStream);
                }
                menuItemVm.Image = @"\images\" + uniqueNameOfString + extension_new;
            }

            var menuItemDto = _viewModelMapper.Map(menuItemVm);

            _menuItemManager.EditMenuItem(menuItemDto);


            return(RedirectToAction(nameof(Index)));
        }