Пример #1
0
        public async Task <IActionResult> Edit(CursoEditViewModel model, IFormFile picture)
        {
            if (ModelState.IsValid)
            {
                if (picture != null)
                {
                    string rutaCursoImagen = Path.Combine(_webHostEnvironment.WebRootPath, "upload/cursos");

                    string archivoUnico = $"{Guid.NewGuid().ToString()}-{Path.GetExtension(picture.FileName)}";

                    string rutaFinal = Path.Combine(rutaCursoImagen, archivoUnico);

                    using (var file = new FileStream(rutaFinal, FileMode.Create))
                    {
                        await picture.CopyToAsync(file);
                    }

                    model.Picture = $"upload/cursos/{archivoUnico}";
                }

                if (await _cursoService.EditarCurso(model))
                {
                    return(RedirectToAction("Listado"));
                }
            }
            return(View(model));
        }
Пример #2
0
        public async Task <bool> EditarCurso(CursoEditViewModel model)
        {
            try
            {
                var curso = _mapper.Map <Curso>(model);
                _contexto.Update(curso);
                await _contexto.SaveChangesAsync();

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }