public async Task <IActionResult> OnPostAsync()
        {
            if (ModelState.IsValid)
            {
                var pathImagesString = _configuration.GetValue <string>("UploadPaths:Images");

                // get last itemId
                var catalogItems = await _catalogItemRepository.ListAllAsync();

                var catalogItem = catalogItems.OrderBy(x => x.Id).LastOrDefault();
                var nextId      = (catalogItem == null?0:catalogItem.Id) + 1;


                var extension = CatalogModel.FormImage.FileName.Substring(CatalogModel.FormImage.FileName.LastIndexOf(".") + 1);
                var nameImg   = $"{nextId}.{extension}";


                var path = $"{_webHostEnvironment.WebRootPath}{pathImagesString}\\{nameImg}".Replace("/", "\\");
                using (var stream = new FileStream(path, FileMode.Create)){
                    await CatalogModel.FormImage.CopyToAsync(stream);

                    //stream.Close();
                }

                CatalogModel.PictureUri = $"{pathImagesString}/{nameImg}";
                CatalogModel.Id         = nextId;

                await _catalogItemViewModelService.AddCatalogItem(CatalogModel);
            }

            return(RedirectToPage("/Admin/Index"));
        }
Ejemplo n.º 2
0
 public async Task <IActionResult> OnPostAsync()
 {
     if (ModelState.IsValid)
     {
         await _catalogItemViewModelService.AddCatalogItem(CatalogModel);
     }
     return(RedirectToPage("/Admin/Index"));
 }
Ejemplo n.º 3
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (ModelState.IsValid)
            {
                var catalogItems = await _itemRepository.ListAllAsync();

                var lastItem = catalogItems.OrderBy(x => x.Id).LastOrDefault();
                var newId    = lastItem.Id + 1;

                CatalogModel.Id = newId;
                await _catalogItemViewModelService.AddCatalogItem(CatalogModel);
            }

            return(RedirectToPage("/Admin/Index"));
        }