public ActionResult View(long treatmentId, Treatment model, [ModelBinder(typeof(DataTablesRequestModelBinder))] DataTablesRequestModel dtRequestModel)
        {
            var treatment = new TreatmentEntity(treatmentId);

            if (treatment.IsNew)
            {
                throw new HttpException(404, SharedRes.Error.NotFound_Treatment);
            }

            // make sure the user has access to this treatment
            if (!Permissions.UserHasPermission("View", treatment))
            {
                throw new HttpException(401, SharedRes.Error.Unauthorized_Treatment);
            }

            // make sure user has access to this page
            if (!RoleUtils.IsUserServiceAdmin() && model.Page != TreatmentPage.Summary && model.Page != TreatmentPage.System &&
                model.Page != TreatmentPage.Definitions)
            {
                throw new HttpException(401, SharedRes.Error.Unauthorized);
            }

            // make sure treatment can be accessed by this user
            model.License = LicenseMode.Full;

            model.Name        = treatment.Patient.FirstName + " " + treatment.Patient.MiddleInitial + " " + treatment.Patient.LastName;
            model.DateOfBirth = treatment.Patient.BirthDate;
            model.Gender      = treatment.Patient.Gender;
            var age = treatment.TreatmentTime.Year - treatment.Patient.BirthDate.Year;

            if (treatment.TreatmentTime < treatment.Patient.BirthDate.AddYears(age))
            {
                age--;
            }
            model.Age       = age;
            model.VisitDate = treatment.TreatmentTime;

            // only load data used for each page
            // severities is used on the summary and the raw report page
            if (model.Page == TreatmentPage.Summary || model.Page == TreatmentPage.RawReport)
            {
                model.Severities =
                    new LinqMetaData().OrganSystemOrgan
                    .Where(x => x.LicenseOrganSystem.LicenseMode == model.License)
                    .OrderBy(x => x.ReportOrder)
                    .OrderBy(x => x.LicenseOrganSystem.ReportOrder)
                    .SelectMany(x => x.Organ.Severities.Where(y => y.TreatmentId == treatmentId))
                    .DistinctBy(x => x.Organ.Description.Replace(" - Left", "").Replace(" - Right", ""));
            }

            // organ systems is only used on the summary page
            if (model.Page == TreatmentPage.Summary)
            {
                model.OrganSystems =
                    new LinqMetaData().LicenseOrganSystem.Where(x => x.LicenseMode == model.License).OrderBy(
                        x => x.ReportOrder).Select(x => x.OrganSystem);

                model.PatientPrescanQuestion = treatment.PatientPrescanQuestion;
            }

            // load all analysis results for the raw data page
            if (model.Page == TreatmentPage.Raw || model.Page == TreatmentPage.Summary)
            {
                model.Raw = new LinqMetaData().AnalysisResult.Where(x => x.TreatmentId == model.TreatmentId);
            }

            // load the debug data for the raw report page
            if (model.Page == TreatmentPage.RawReport)
            {
                model.Debug = GetDebugData(new LinqMetaData().CalculationDebugData.Where(x => x.TreatmentId == model.TreatmentId), model.Severities, model.License);

                model.NBScores = new LinqMetaData().NBAnalysisResult.Where(x => x.TreatmentId == model.TreatmentId);
            }

            // only load images for the images page
            if (!Request.IsAjaxRequest() && !ControllerContext.IsChildAction)
            {
                // get database images
                var energizedImages   = Utilities.Treatment.ImageRetrievalHelper.GetPatientImages(treatment.EnergizedImageSetId);
                var calibrationImages = Utilities.Treatment.ImageRetrievalHelper.GetCalibrationImageSet(treatment.CalibrationId);

                // save in cache for a few minutes
                var caches = new LinqMetaData().ImageCache.Where(
                    x => x.LookupKey == treatmentId &&
                    (x.Description.StartsWith("Finger-") || x.Description.StartsWith("Calibration-"))).Select(x => x.Description).ToList();

                // save extracted images to database
                for (var i = 0; i < energizedImages.Count; i++)
                {
                    if (caches.All(x => x != "Finger-" + i))
                    {
                        using (var mem = new MemoryStream())
                        {
                            energizedImages[i].Image.Save(mem, ImageFormat.Png);
                            new ImageCacheEntity
                            {
                                LookupKey   = treatmentId,
                                Description = "Finger-" + i,
                                Image       = mem.ToArray()
                            }.Save();
                            energizedImages[i].Image.Dispose();
                        }
                    }
                }
                for (var i = 0; i < calibrationImages.Count; i++)
                {
                    if (caches.All(x => x != "Calibration-" + i))
                    {
                        using (var mem = new MemoryStream())
                        {
                            calibrationImages[i].Image.Save(mem, ImageFormat.Png);
                            new ImageCacheEntity
                            {
                                LookupKey   = treatmentId,
                                Description = "Calibration-" + i,
                                Image       = mem.ToArray()
                            }.Save();
                            calibrationImages[i].Image.Dispose();
                        }
                    }
                }
            }

            ViewResult result = View(model);

            if (dtRequestModel == null)
            {
                return(result);
            }

            return(Query(result, dtRequestModel));
        }