Exemplo n.º 1
0
        public IActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                _workContainer.category.add(category);
                _workContainer.save();
                return(RedirectToAction(nameof(Index)));
            }

            return(View(category));
        }
Exemplo n.º 2
0
        public IActionResult Edit(ArticleVM articleVM)
        {
            if (ModelState.IsValid)
            {
                Article art = _workContainer.article.Get(articleVM.Article.Id);

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

                if (files.Count() > 0)
                {
                    // Editar imagen
                    string fileName  = Guid.NewGuid().ToString();
                    string Uploads   = Path.Combine(mainRoute, @"images\articles");
                    string extension = Path.GetExtension(files[0].FileName);

                    //Eliminar imagen
                    string imageOld = Path.Combine(mainRoute, art.UrlImage.TrimStart('\\'));
                    if (System.IO.File.Exists(imageOld))
                    {
                        System.IO.File.Delete(imageOld);
                    }

                    // Subir nuevament la imagen
                    using (FileStream fs = new FileStream(Path.Combine(Uploads, $"{fileName}{extension}"), FileMode.Create))
                    {
                        files[0].CopyTo(fs);
                    };

                    articleVM.Article.UrlImage = @$ "\images\articles\{fileName}{extension}";
                }
                _workContainer.article.update(articleVM.Article);
                _workContainer.save();

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