public void UpdateMedicalProfileForPatient(FormCollection formCollection)
 {
     int medicalProfileTemplateId = Int32.Parse(formCollection["medicalProfileTemplateId"]);
     int patientId = Int32.Parse(formCollection["patientId"]);
     var medicalProfile = _db.MedicalProfiles.Where(
             x => ((x.MedicalProfileTemplateId == medicalProfileTemplateId)
             && (x.PatientId == patientId))).FirstOrDefault();
     foreach (string _formData in formCollection)
     {
         if (!_formData.Equals("medicalProfileTemplateId") && !_formData.Equals("patientId"))
         {
             Debug.WriteLine("Element: " + _formData + ". Form data: " + formCollection[_formData]);
             int customSnippetId = Int32.Parse(_formData);
             if (customSnippetId > 0)
             {
                 var customSnippetValue = _db.CustomSnippetValues.Where(
                     x => (x.CustomSnippetId == customSnippetId)
                         && (x.MedicalProfileId == medicalProfile.MedicalProfileId)
                     ).FirstOrDefault();
                 if (customSnippetValue == null)
                 {
                     customSnippetValue = new CustomSnippetValue
                     {
                         MedicalProfileId = medicalProfile.MedicalProfileId,
                         CustomSnippetId = customSnippetId
                     };
                     _db.CustomSnippetValues.Add(customSnippetValue);
                 }
                 string[] value = formCollection.GetValues(_formData);
                 if (value.Length == 1)
                 {
                     customSnippetValue.Value = value[0];
                 }
                 else
                 {
                     customSnippetValue.Value = String.Join("~~", value);
                 }
                 _db.SaveChanges();
             }
         }
     }
 }
        public string GetValueForSnippet(CustomSnippet customSnippet, int patientId, int medicalProfileId)
        {
            string value = "";

            //First, try to get data from CustomSnippetValue
            var customSnippetValue = _db.CustomSnippetValues.Where(
                        x => (x.CustomSnippetId == customSnippet.CustomSnippetId)
                        && (x.MedicalProfileId == medicalProfileId)).FirstOrDefault();
            if (customSnippetValue == null)
            {
                customSnippetValue = new CustomSnippetValue
                {
                    CustomSnippet = customSnippet,
                    MedicalProfileId = medicalProfileId
                };
                _db.CustomSnippetValues.Add(customSnippetValue);
                _db.SaveChanges();
            }
            value = customSnippetValue.Value;

            var user = _db.Users.Where(pa => pa.UserId == patientId).SingleOrDefault();
            var patient = _db.Patients.Where(pa => pa.UserId == patientId).SingleOrDefault();
            var personalHealthRecord = _db.PersonalHealthRecords.Where(pa => pa.PatientId == patientId).SingleOrDefault();
            //Use reflection to get binding data
            if (customSnippet.SnippetType != SnippetType.Custom && String.IsNullOrEmpty(value))
            {
                value = "";
                switch (customSnippet.SnippetType)
                {
                    case SnippetType.User:
                        Type type = typeof(User);
                        object valueProperty;
                        if (customSnippet.SnippetFieldName.Equals("Age"))
                        {
                            valueProperty = type.GetProperty
                                ("Birthday", BindingFlags.IgnoreCase
                                | BindingFlags.Public
                                | BindingFlags.Instance).GetValue(user, null);
                            if (valueProperty != null)
                            {
                                value = valueProperty.ToString();
                                var birthday = DateTime.Parse(value.ToString());
                                value = DateTimeUtils.CalculateAge(birthday).ToString();
                            }
                        }
                        else
                        {
                            valueProperty = type.GetProperty
                                (customSnippet.SnippetFieldName, BindingFlags.IgnoreCase
                                | BindingFlags.Public
                                | BindingFlags.Instance).GetValue(user, null);
                            if (valueProperty != null)
                            {
                                value = valueProperty.ToString();
                                if ("Gender".Equals(customSnippet.SnippetFieldName))
                                {
                                    value = "Nam".Equals(value) ? "Nam" : "Nữ";
                                }
                                if (customSnippet.SnippetFieldName.Equals("Birthday"))
                                {
                                    var birthday = DateTime.Parse(value.ToString());
                                    value = birthday.ToString("dd/MM/yyyy");
                                }
                            }
                        }
                        break;
                    case SnippetType.Patient:
                        type = typeof(Patient);
                        valueProperty = type.GetProperty
                            (customSnippet.SnippetFieldName, BindingFlags.IgnoreCase
                            | BindingFlags.Public
                            | BindingFlags.Instance).GetValue(patient, null);
                        if (valueProperty != null) {
                            value = valueProperty.ToString();
                        }
                        break;
                    case SnippetType.PersonalHealthRecord:
                        type = typeof(PersonalHealthRecord);
                        valueProperty = type.GetProperty
                            (customSnippet.SnippetFieldName, BindingFlags.IgnoreCase
                            | BindingFlags.Public
                            | BindingFlags.Instance).GetValue(personalHealthRecord, null);
                        if (valueProperty != null)
                        {
                            value = valueProperty.ToString();
                        }
                        break;
                }
            }
            return value;
        }