public async Task <IActionResult> Create(/*[Bind("IdCandidatos,Nombre,Apellido,PartidoPertenece,PuestoAspira,FotoPerfil,Estado")] Candidatos candidatos,*/ CandidatosLDTO model) { var candidato = new Candidatos(); if (ModelState.IsValid) { string uniqueName = null; if (model.Foto != null) { var folderPath = Path.Combine(hostingEnvironment.WebRootPath, "images"); uniqueName = Guid.NewGuid().ToString() + "_" + model.Foto.FileName; var filePath = Path.Combine(folderPath, uniqueName); if (filePath != null) { model.Foto.CopyTo(new FileStream(filePath, mode: FileMode.Create)); } } candidato = _mapper.Map <Candidatos>(model); candidato.FotoPerfil = uniqueName; _context.Add(candidato); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } /* ViewData["PartidoPertenece"] = new SelectList(_context.Partidos, "IdPartidos", "Descripcion", candidatos.PartidoPertenece); * ViewData["PuestoAspira"] = new SelectList(_context.PuestoElecto, "IdPuestoE", "Descripcion", candidatos.PuestoAspira);*/ return(View(model)); }
public async Task <IActionResult> Edit(int id, CandidatosLDTO lDTO) { if (id != lDTO.IdCandidatos) { return(NotFound()); } if (ModelState.IsValid) { try { var candidato = await _context.Candidatos.FirstOrDefaultAsync(d => d.IdCandidatos == lDTO.IdCandidatos); string uniqueName = null; if (lDTO.Foto != null) { var folderPath = Path.Combine(hostingEnvironment.WebRootPath, "images"); uniqueName = Guid.NewGuid().ToString() + "_" + lDTO.Foto.FileName; var filePath = Path.Combine(folderPath, uniqueName); if (!string.IsNullOrEmpty(candidato.FotoPerfil)) { var filePathEliminar = Path.Combine(folderPath, candidato.FotoPerfil); if (System.IO.File.Exists(filePathEliminar)) { //var fileInfo = new System.IO.FileInfo(filePathEliminar); //fileInfo.Delete(); System.IO.File.Delete(filePathEliminar); } } //el problema qcre que esta en esta linea, la voy a cambiar ok? dale if (filePath != null) { using (var fs = new FileStream(filePath, FileMode.Create)) { lDTO.Foto.CopyTo(fs); } } //if (filePath != null) lDTO.Foto.CopyTo(new FileStream(filePath, mode: FileMode.Create)); } candidato.Nombre = lDTO.Nombre; candidato.Apellido = lDTO.Apellido; candidato.PartidoPertenece = lDTO.PartidoPertenece; candidato.PuestoAspira = lDTO.PuestoAspira; candidato.FotoPerfil = uniqueName; candidato.Estado = lDTO.Estado; _context.Update(candidato); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CandidatosExists(lDTO.IdCandidatos)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } /*ViewData["PartidoPertenece"] = new SelectList(_context.Partidos, "IdPartidos", "Descripcion", candidatos.PartidoPertenece); * ViewData["PuestoAspira"] = new SelectList(_context.PuestoElecto, "IdPuestoE", "Descripcion", candidatos.PuestoAspira);*/ return(View(lDTO)); }