public async Task <IActionResult> Create(CentroAsistencialViewModel model) { if (ModelState.IsValid) { var path = string.Empty; if (model.LogoFile != null) { path = await _imageHelper.UpLoadImageAsync(model.LogoFile, "Centros"); } CentroAsistencial centroAsistencial = _converterHelper.ToCentroAsistencial(model, path, true); _context.Add(centroAsistencial); try { await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, $"Centro Asistencial {centroAsistencial.Nombre} ya existe"); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); } } } return(View(model)); }
public CentroAsistencial ToCentroAsistencial(CentroAsistencialViewModel model, string path, bool isNew) { return(new CentroAsistencial { Equipos = model.Equipos, Id = isNew ? 0 : model.Id, Logo = path, Nombre = model.Nombre }); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } CentroAsistencial centroAsistencial = await _context.CentrosAsistenciales.FindAsync(id); if (centroAsistencial == null) { return(NotFound()); } CentroAsistencialViewModel centroAsistencialViewModel = _converterHelper.ToCentroAsistencialViewModel(centroAsistencial); return(View(centroAsistencialViewModel)); }
public async Task <IActionResult> Edit(int id, CentroAsistencialViewModel model) { if (id != model.Id) { return(NotFound()); } if (ModelState.IsValid) { var path = model.Logo; if (model.LogoFile != null) { path = await _imageHelper.UpLoadImageAsync(model.LogoFile, "Centros"); } CentroAsistencial entity = _converterHelper.ToCentroAsistencial(model, path, false); _context.Update(entity); try { await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, $"Centro Asistencial {model.Nombre} ya existe"); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); } } } return(View(model)); }