public async Task <ActionResult> ConfirmAddOrEdit(InterestRegion model) { if (ModelState.IsValid == false) { return(View("AddOrEdit", model)); } if (model.Id > 0) { var offe = await db.InterestRegions.FirstOrDefaultAsync(x => x.Name == model.Name && x.Id != model.Id); if (offe == null) { var interestRegion = await db.InterestRegions.FindAsync(model.Id); if (interestRegion != null) { interestRegion.Name = model.Name; interestRegion.Active = model.Active; interestRegion.MailCollection = model.MailCollection; db.Entry(interestRegion).State = EntityState.Modified; await db.SaveChangesAsync(); } } } else { var region = await db.InterestRegions.FirstOrDefaultAsync(a => a.Name == model.Name); if (region == null) { region = new InterestRegion() { CreationDate = DateTime.Now, Name = model.Name, Active = model.Active, MailCollection = model.MailCollection }; db.InterestRegions.Add(region); await db.SaveChangesAsync(); } else { ModelState.AddModelError("", "Ya existe una región con el nombre " + model.Name); return(View("AddOrEdit", model)); } } return(RedirectToAction("Index")); }
public async Task <ActionResult> Delete(int?id) { try { InterestRegion interestRegion = await db.InterestRegions.FindAsync(id); db.InterestRegions.Remove(interestRegion); await db.SaveChangesAsync(); //return Json(new AjaxResponse { Success = true, Message = "La Categoría se eliminó correctamente." }, JsonRequestBehavior.AllowGet); return(RedirectToAction("Index")); } catch (Exception) { //return Json(new AjaxResponse { Success = false, Message = "No se puede eliminar la categoría, está siendo utilizada por otra instancia." }, JsonRequestBehavior.AllowGet); } return(RedirectToAction("NotFound", "Error")); }