Example #1
0
        public async Task <IActionResult> Create(/*[Bind("IdPartidos,Nombre,Descripcion,LogoPartido,Estado")] Partidos partidos,*/ PartidosLDTO model)
        {
            var partido = new Partidos();

            if (ModelState.IsValid)
            {
                string uniqueName = null;
                if (model.Logo != null)
                {
                    var folderPath = Path.Combine(hostingEnvironment.WebRootPath, "images");
                    uniqueName = Guid.NewGuid().ToString() + "_" + model.Logo.FileName;
                    var filePath = Path.Combine(folderPath, uniqueName);

                    if (filePath != null)
                    {
                        model.Logo.CopyTo(new FileStream(filePath, mode: FileMode.Create));
                    }
                }

                partido = _mapper.Map <Partidos>(model);

                partido.LogoPartido = uniqueName;

                _context.Add(partido);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
Example #2
0
        public async Task <IActionResult> Edit(int id, /* [Bind("IdPartidos,Nombre,Descripcion,LogoPartido,Estado")] Partidos partidos*/ PartidosLDTO lDTO)
        {
            if (id != lDTO.IdPartidos)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var partido = await _context.Partidos.FirstOrDefaultAsync(d => d.IdPartidos == lDTO.IdPartidos);


                    string uniqueName = null;
                    if (lDTO.Logo != null)
                    {
                        var folderPath = Path.Combine(hostingEnvironment.WebRootPath, "images");
                        uniqueName = Guid.NewGuid().ToString() + "_" + lDTO.Logo.FileName;
                        var filePath = Path.Combine(folderPath, uniqueName);



                        if (!string.IsNullOrEmpty(partido.LogoPartido))
                        {
                            var filePathEliminar = Path.Combine(folderPath, partido.LogoPartido);
                            if (System.IO.File.Exists(filePathEliminar))
                            {
                                var fileInfo = new System.IO.FileInfo(filePathEliminar);
                                fileInfo.Delete();
                            }
                        }


                        if (filePath != null)
                        {
                            lDTO.Logo.CopyTo(new FileStream(filePath, mode: FileMode.Create));
                        }
                    }

                    partido.Nombre      = lDTO.Nombre;
                    partido.Descripcion = lDTO.Descripcion;
                    partido.LogoPartido = uniqueName;
                    partido.Estado      = lDTO.Estado;



                    _context.Update(partido);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PartidosExists(lDTO.IdPartidos))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(lDTO));
        }