コード例 #1
0
        public JsonResult Create(Paciente paciente)
        {
            try
            {
                if (ModelState.IsValid)
                {

                    if (paciente.PacienteTelefone.Count == 0)
                    {
                        return Json(new { Success = 0, ex = "O paciente deve possuir ao menos um telefone cadastrado!" });
                    }

                    using (var db = new ClinicaEntities())
                    {
                        // Se o código do paciente é maior que zero, entendemos que existe registro para tal.
                        // Então nós "atualizaremos" ele.
                        if (paciente.PacienteId > 0)
                        {

                            var selPacienteTelefone = db.PacienteTelefone.Where(p => p.PacienteId == paciente.PacienteId);

                            foreach (PacienteTelefone pt in selPacienteTelefone)
                            {
                                db.PacienteTelefone.Remove(pt);
                            }

                            foreach (PacienteTelefone pt in paciente.PacienteTelefone)
                            {
                                db.PacienteTelefone.Add(pt);
                            }

                            db.Entry(paciente).State = EntityState.Modified;

                        }
                        //Perform Save
                        else
                        {
                            db.Paciente.Add(paciente);
                        }

                        db.SaveChanges();

                    }

                    return Json(new { Success = 1, SalesID = paciente.PacienteId, ex = "" });
                }
                else
                {
                    ValidateModel(paciente);
                }
            }
            catch (Exception ex)
            {

                if (ex.InnerException != null)
                {
                    return Json(new { Success = 0, ex = ex.InnerException.ToString() });
                }
                else
                {
                    return Json(new { Success = 0, ex = ex.Message.ToString() });
                }
            }

            return Json(new { Success = 0, ex = new Exception("Unable to save").Message.ToString() });
        }
コード例 #2
0
        public ActionResult Edit(int id, Paciente paciente)
        {
            ViewBag.Title = tituloCadastro;
            try
            {
                ClinicaEntities db = new ClinicaEntities();

                try
                {
                    db.Entry(paciente).State = System.Data.EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                finally
                {
                    db.Dispose();
                }
            }
            catch (Exception e)
            {
                ViewBag.Error = e;
                return View("Error");
            }
        }
コード例 #3
0
 private static void CarregarAssociacoes(ClinicaEntities db, Paciente paciente)
 {
     db.Entry(paciente).Collection("PacienteTelefone").Load();
 }
コード例 #4
0
 public ActionResult Delete(int id, Paciente paciente)
 {
     ViewBag.Title = tituloCadastro;
     try
     {
         using (var db = new ClinicaEntities())
         {
             db.Entry(paciente).State = System.Data.EntityState.Deleted;
             db.SaveChanges();
         }
         return RedirectToAction("Index");
     }
     catch (Exception e)
     {
         /*ViewBag.Error = e;*/
         TempData["Error"] = e;
         return RedirectToAction("Error", "Error");
         /*return View("Error");*/
     }
 }