private void saveAssessment(LessonFitnessAssessment fitnessAssessment, string key)
        {
            int itemID = int.Parse(key.Substring(1));

            var values = Request.Form[key];

            if (values == (String)null || values.Count == 0)
            {
                return;
            }

            var isNew = false;
            var item  = models.GetTable <LessonFitnessAssessmentReport>()
                        .Where(r => r.AssessmentID == fitnessAssessment.AssessmentID && r.ItemID == itemID).FirstOrDefault();

            if (item == null)
            {
                item = new LessonFitnessAssessmentReport
                {
                    AssessmentID = fitnessAssessment.AssessmentID,
                    ItemID       = itemID
                };

                isNew = true;
            }

            decimal decVal;
            int     intVal;

            if (values.Count > 1 && decimal.TryParse(values[0], out decVal) && int.TryParse(values[1], out intVal))
            {
                item.SingleAssessment = decVal;
                item.ByTimes          = intVal;
                fitnessAssessment.LessonFitnessAssessmentReport.Add(item);
            }
            else if (values.Count > 0 && decimal.TryParse(values[0], out decVal))
            {
                item.TotalAssessment = decVal;
                fitnessAssessment.LessonFitnessAssessmentReport.Add(item);
            }
            else
            {
                if (!isNew)
                {
                    models.GetTable <LessonFitnessAssessmentReport>().DeleteOnSubmit(item);
                }
            }
        }
        public async Task <ActionResult> UpdateAssessmentReportAsync(FitnessAssessmentReportViewModel viewModel)
        {
            var profile = await HttpContext.GetUserAsync();

            var fitnessAssessment = models.GetTable <LessonFitnessAssessment>().Where(f => f.AssessmentID == viewModel.AssessmentID).FirstOrDefault();

            if (fitnessAssessment == null)
            {
                return(Json(new { result = false }));
            }

            var item = fitnessAssessment.LessonFitnessAssessmentReport.Where(r => r.ItemID == viewModel.TrendItem).FirstOrDefault();

            if (item == null)
            {
                item = new LessonFitnessAssessmentReport
                {
                    ItemID = viewModel.TrendItem
                };
                fitnessAssessment.LessonFitnessAssessmentReport.Add(item);
            }

            item.TotalAssessment = viewModel.TrendAssessment;

            if (viewModel.ItemID.HasValue)
            {
                var fitnessItem = models.GetTable <FitnessAssessmentItem>().Where(f => f.ItemID == viewModel.ItemID).FirstOrDefault();
                if (fitnessItem != null)
                {
                    item = fitnessAssessment.LessonFitnessAssessmentReport.Where(r => r.ItemID == viewModel.ItemID).FirstOrDefault();
                    if (item == null)
                    {
                        item = new LessonFitnessAssessmentReport
                        {
                            ItemID = viewModel.ItemID.Value
                        };
                        fitnessAssessment.LessonFitnessAssessmentReport.Add(item);
                    }

                    if (viewModel.Calc == "total")
                    {
                        item.TotalAssessment = viewModel.TotalAssessment;
                    }
                    else
                    {
                        item.TotalAssessment  = null;
                        item.SingleAssessment = viewModel.SingleAssessment;
                        item.ByTimes          = viewModel.ByTimes;
                    }

                    if (fitnessItem.UseCustom == true)
                    {
                        item.ByCustom = viewModel.ByCustom;
                    }
                    if (fitnessItem.UseSingleSide == true)
                    {
                        item.BySingleSide = viewModel.BySingleSide;
                    }
                }
            }

            models.SubmitChanges();

            if (models.CouldMarkToAttendLesson(fitnessAssessment.LessonTime))
            {
                models.AttendLesson(fitnessAssessment.LessonTime, profile);
                //foreach (var r in fitnessAssessment.LessonTime.GroupingLesson.RegisterLesson)
                //{
                //    models.CheckLearnerQuestionnaireRequest(r);
                //}
            }


            return(Json(new { result = true }));
        }