public IActionResult InsertOrUpdateTeachingFunction(TeachingFunction teachingFunction) { if (ModelState.IsValid) { Action action = Action.None; if (teachingFunction.Id == 0) { action = Action.Create; _unitWork.TeachingFunction.Add(teachingFunction); } else { action = Action.Update; _unitWork.TeachingFunction.Update(teachingFunction); } try { _unitWork.Save(); if (action == Action.Create) { _notyfService.Success("Función docente creada correctamente."); } if (action == Action.Update) { _notyfService.Success("Función docente actualizada correctamente."); } return(RedirectToAction(nameof(Index))); } catch (DbUpdateException dbUpdateException) { if (dbUpdateException.InnerException.Message.Contains("IX_TeachingFunctions_Name")) { _notyfService.Error("Ya existe una función docente con el mismo nombre."); return(View(teachingFunction)); } else { ModelState.AddModelError(string.Empty, dbUpdateException.InnerException.Message); } } catch (Exception exception) { ModelState.AddModelError(string.Empty, exception.Message); } } return(View(teachingFunction)); }
public IActionResult InsertOrUpdateTeachingFunction(int?id) { TeachingFunction teachingFunction = new TeachingFunction(); if (id == null) { teachingFunction.Active = true; // Crea un nuevo registro return(View(teachingFunction)); } // Actualiza el registro teachingFunction = _unitWork.TeachingFunction.Get(id.GetValueOrDefault()); if (teachingFunction == null) { return(NotFound()); } return(View(teachingFunction)); }