public ActionResult Edit([Bind(Include = "id,fecha,causa,animal_id")] muerte muerte)
 {
     if (ModelState.IsValid)
     {
         var anim = db.animal.Where(a => a.id == muerte.animal_id).FirstOrDefault();
         if (anim.fec_nac == null)
         {
             db.Entry(muerte).State = EntityState.Modified;
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
         else
         {
             if (anim.fec_nac > muerte.fecha)
             {
                 ViewBag.Error = "Fecha de muerte no debe ser antes de la fecha de nacimiento del animal";
             }
             else
             {
                 db.Entry(muerte).State = EntityState.Modified;
                 db.SaveChanges();
                 return(RedirectToAction("Index"));
             }
         }
     }
     ViewBag.animal_id = new SelectList(db.animal, "id", "codigo_sag", muerte.animal_id);
     return(View(muerte));
 }
        // GET: Muerte/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            muerte muerte = db.muerte.Find(id);

            if (muerte == null)
            {
                return(HttpNotFound());
            }
            return(View(muerte));
        }
        // GET: Muerte/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            muerte muerte = db.muerte.Find(id);

            if (muerte == null)
            {
                return(HttpNotFound());
            }
            ViewBag.animal_id = new SelectList(db.animal, "id", "codigo_sag", muerte.animal_id);
            return(View(muerte));
        }
        public ActionResult Create([Bind(Include = "id,fecha,causa,animal_id")] muerte muerte)
        {
            if (ModelState.IsValid)
            {
                var anim = db.animal.Where(a => a.id == muerte.animal_id).FirstOrDefault();
                if (anim.fec_nac == null)
                {
                    int sag = muerte.animal_id;

                    animal ani = db.animal.Where(a => a.id == sag).FirstOrDefault();
                    estado est = db.estado.Where(e => e.nombre == "MUERTO").FirstOrDefault();

                    ani.estado = est;

                    db.muerte.Add(muerte);
                    db.SaveChanges();


                    return(RedirectToAction("Index"));
                }
                else
                {
                    if (anim.fec_nac > muerte.fecha)
                    {
                        ViewBag.Error = "Fecha de muerte no debe ser antes de la fecha de nacimiento del animal";
                    }
                    else
                    {
                        int sag = muerte.animal_id;

                        animal ani = db.animal.Where(a => a.id == sag).FirstOrDefault();
                        estado est = db.estado.Where(e => e.nombre == "MUERTO").FirstOrDefault();

                        ani.estado = est;
                        db.muerte.Add(muerte);
                        db.SaveChanges();
                        return(RedirectToAction("Index"));
                    }
                }
            }

            ViewBag.animal_id = new SelectList(db.animal, "id", "codigo_sag", muerte.animal_id);
            return(View(muerte));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            muerte muerte = db.muerte.Find(id);

            try
            {
                int sag = muerte.animal_id;

                animal ani = db.animal.Where(a => a.id == sag).FirstOrDefault();
                estado est = db.estado.Where(e => e.nombre == "NORMAL").FirstOrDefault();

                ani.estado = est;
                db.muerte.Remove(muerte);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            catch
            {
                ViewBag.Error = "No se puede eliminar debido a que existen datos asociados";
            }
            return(View(muerte));
        }