public ActionResult Edit(ExaminationResultViewModel[] examResults)
        {
            var formModel = examResults.Single();

            ExaminationResult dbObject;

            if (formModel.Id == null)
            {
                dbObject = new ExaminationResult
                {
                    CreatedOn = this.GetUtcNow(),
                    PatientId = formModel.PatientId.Value,
                    PracticeId = this.DbUser.PracticeId,
                };

                this.db.ExaminationResults.AddObject(dbObject);
            }
            else
            {
                var practiceId = this.DbUser.PracticeId;

                dbObject = this.db.ExaminationResults
                    .Where(r => r.Id == formModel.Id)
                    .FirstOrDefault(r => r.Patient.Doctor.Users.FirstOrDefault().PracticeId == practiceId);

                // If modelObj is null, we must tell the user that this object does not exist.
                if (dbObject == null)
                    return View("NotFound", formModel);

                // Security issue... must check current user practice against the practice of the edited objects.
                var currentUser = this.DbUser;
                if (currentUser.PracticeId != dbObject.Patient.Doctor.Users.First().PracticeId)
                    return View("NotFound", formModel);
            }

            if (this.ModelState.IsValid)
            {
                dbObject.Patient.IsBackedUp = false;
                dbObject.Text = formModel.Text;
                dbObject.MedicalProcedureName = formModel.MedicalProcedureName;
                dbObject.MedicalProcedureCode = formModel.MedicalProcedureId.HasValue
                    ? this.db.SYS_MedicalProcedure.Where(mp => mp.Id == formModel.MedicalProcedureId).Select(mp => mp.Code).FirstOrDefault()
                    : formModel.MedicalProcedureCode;
                dbObject.ExaminationDate = this.ConvertToUtcDateTime(formModel.ExaminationDate.Value);
                dbObject.ReceiveDate = this.ConvertToUtcDateTime(formModel.ReceiveDate.Value);

                this.db.SaveChanges();

                return this.View("Details", GetViewModel(dbObject, this.GetToLocalDateTimeConverter()));
            }

            return this.View("Edit", formModel);
        }
 public ActionResult Create(ExaminationResultViewModel[] examResults)
 {
     return this.Edit(examResults);
 }
        public ActionResult Edit(int? id, int? patientId, int? y, int? m, int? d)
        {
            ExaminationResultViewModel viewModel;

            if (id != null)
            {
                var practiceId = this.DbUser.PracticeId;

                var modelObj = this.db.ExaminationResults
                    .Where(r => r.Id == id)
                    .FirstOrDefault(r => r.Patient.Doctor.Users.FirstOrDefault().PracticeId == practiceId);

                // todo: if modelObj is null, we must tell the user that this object does not exist.

                viewModel = GetViewModel(modelObj, this.GetToLocalDateTimeConverter());
            }
            else
            {
                viewModel = new ExaminationResultViewModel
                    {
                        Id = null,
                        PatientId = patientId,
                        ExaminationDate = null,
                        ReceiveDate = DateTimeHelper.CreateDate(y, m, d) ?? this.GetPracticeLocalNow(),
                    };
            }

            return this.View("Edit", viewModel);
        }