public async Task <IActionResult> Create(ProfesorEntity profesorEntity) { if (ModelState.IsValid) { try { //Convertimos el DNI en mayúsculas profesorEntity.DNI = profesorEntity.DNI.ToUpper(); _context.Add(profesorEntity); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, "Ya existe un profesor con ese DNI"); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); } } } return(View(profesorEntity)); }
public async Task <IActionResult> Edit(int id, ProfesorEntity profesorEntity) { if (id != profesorEntity.ID) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(profesorEntity); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ex) { if (!ProfesorEntityExists(profesorEntity.ID)) { ModelState.AddModelError(string.Empty, "No existe este DNI"); } if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, "Ya existe un profesor con ese DNI"); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); } } return(RedirectToAction(nameof(Index))); } return(View(profesorEntity)); }
public async Task <IActionResult> Delete(int?id) { if (id == null) { return(NotFound()); } ProfesorEntity profesorEntity = await _context.Profesor .FirstOrDefaultAsync(m => m.ID == id); if (profesorEntity == null) { return(NotFound()); } _context.Profesor.Remove(profesorEntity); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }