Ejemplo n.º 1
0
        public ActionResult Edit(State state)
        {
            if (!ModelState.IsValid)
            {
                return View(state);
                   
            }      
            db.Entry(state).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");

        }
Ejemplo n.º 2
0
        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");
        }
Ejemplo n.º 3
0
        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");         
        }
Ejemplo n.º 4
0
        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;

        }