public ActionResult Edit(State state) { if (!ModelState.IsValid) { return View(state); } db.Entry(state).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); }
public ActionResult Create(State state) { //Valido si el modelo es valido: if (!ModelState.IsValid) { return View(state); } db.States.Add(state); db.SaveChanges(); //Lo redirecciono a la vista Index(o a la que to desee) return RedirectToAction("Index"); }
public ActionResult Delete(int id, State state) { state = db.States.Find(id); if (state == null) { return HttpNotFound(); } db.States.Remove(state); try { db.SaveChanges(); } catch (Exception ex) { //Error reference cuasado por claves primarias: if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("REFERENCE")) { ViewBag.Error = "Can't delete the record, because has related records....."; return View(state); } else { ViewBag.Error = ex.Message; return View(state); } //return View(state); } return RedirectToAction("Index"); }
private State GetState(string stateName) { //buscar en la tabla estado este (open)abierto: var state = db.States.Where(s => s.Description == stateName).FirstOrDefault(); //valido si el estado existe: if (state == null) { //creo el estado open, y me aseguro que siempre lo este: state = new State { Description = stateName, }; db.States.Add(state); db.SaveChanges(); } return state; }