private void updateScores(tblSWATWAMonthlyQuantity tblswatwamonthlyquantity)
        {
            int?[] monthlyQuantities = {tblswatwamonthlyquantity.January, tblswatwamonthlyquantity.February, tblswatwamonthlyquantity.March,
                                        tblswatwamonthlyquantity.April, tblswatwamonthlyquantity.May, tblswatwamonthlyquantity.June,
                                        tblswatwamonthlyquantity.July, tblswatwamonthlyquantity.August, tblswatwamonthlyquantity.September,
                                        tblswatwamonthlyquantity.October, tblswatwamonthlyquantity.November, tblswatwamonthlyquantity.December
                                       };
            int? wamqTot = null;
            foreach (int? monthlyQuanity in monthlyQuantities)
            {
                if (monthlyQuanity != null)
                {
                    if (db.lkpSWATwaterMonthLUs.Find(monthlyQuanity).intorder < 3)
                    {
                        wamqTot = wamqTot.GetValueOrDefault(0) + 1;
                    }
                }
            }
            double? waterScore = null;
            if (wamqTot != null)
            {
                waterScore = wamqTot / 12.0;
            }

            db.tblSWATScores.Single(e => e.SurveyID == tblswatwamonthlyquantity.SurveyID && e.VarName == "waterTot").Value = wamqTot;
            db.tblSWATScores.Single(e => e.SurveyID == tblswatwamonthlyquantity.SurveyID && e.VarName == "waterSCORE").Value = waterScore;
            db.SaveChanges();
        }
        public ActionResult Edit([Bind(Include="ID,SurveyID,January,February,March,April,May,June,July,August,September,October,November,December")] tblSWATWAPrecipitation tblswatwaprecipitation)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tblswatwaprecipitation).State = EntityState.Modified;
                db.SaveChanges();
                updateScores(tblswatwaprecipitation);

                // If there is not any WAMonthlyQuantity with the current survey (SurveyID) then create one and redirecto to its edit link.
                var tblswatwamonthlyquantity = db.tblSWATWAMonthlyQuantities.Where(e => e.SurveyID == tblswatwaprecipitation.SurveyID);
                if (!tblswatwamonthlyquantity.Any())
                {
                    tblSWATWAMonthlyQuantity wamq = new tblSWATWAMonthlyQuantity();
                    wamq.SurveyID = tblswatwaprecipitation.SurveyID;
                    db.tblSWATWAMonthlyQuantities.Add(wamq);
                    db.SaveChanges();
                    int newWamqId = wamq.ID;
                    return RedirectToAction("Edit", "WAMonthlyQuantity", new { id = newWamqId, SurveyID = wamq.SurveyID });
                }
                else
                {
                    return RedirectToAction("Edit", "WAMonthlyQuantity", new { id = tblswatwamonthlyquantity.Single(e => e.SurveyID == tblswatwaprecipitation.SurveyID).ID, SurveyID = tblswatwaprecipitation.SurveyID });
                }

                //return RedirectToAction("Index");
            }
            return View(tblswatwaprecipitation);
        }