Beispiel #1
0
        public async Task <IActionResult> Create([Bind("ID,Nombre,Description,Image")] GaleryViewModel model, IFormFile img)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(RedirectToAction(nameof(AccountController.Login), "Account"));
            }

            if (ModelState.IsValid)
            {
                var newPicture = new GaleryData();
                var image      = img;

                if (image != null)
                {
                    var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", image.FileName);

                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await image.CopyToAsync(stream);
                    }
                    newPicture.Image = image.FileName;
                }
                else
                {
                    var path = "default.png";
                    newPicture.Image = path;
                }

                newPicture.Nombre      = model.Nombre;
                newPicture.Description = model.Description;
                newPicture             = _empleadosData.AddPicture(newPicture);
                var notification = new Notifications();

                TempData["sms"]      = "Campo añadido correctamente";
                ViewBag.sms          = TempData["sms"];
                notification.Detalle = ViewBag.sms;
                notification.Section = "Galeria";
                notification.Tipo    = "check";
                notification.Time    = DateTime.Now;
                notification         = _empleadosData.AddNotification(notification);
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
        public async Task <IActionResult> Create([Bind("ID,Nombre,Description,Image")] GaleryViewModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(RedirectToAction(nameof(AccountController.Login), "Account"));
                // throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }


            if (ModelState.IsValid)
            {
                var newPicture = new GaleryData();
                var image      = model.Image;

                if (image != null)
                {
                    var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", image.FileName);

                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await image.CopyToAsync(stream);
                    }
                    newPicture.Image = image.FileName;
                }
                else
                {
                    var path = "default.png";
                    newPicture.Image = path;
                }

                newPicture.Nombre      = model.Nombre;
                newPicture.Description = model.Description;

                newPicture = _empleadosData.AddPicture(newPicture);
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }