public ActionResult Edit([Bind(Include="ID,SurveyID,January,February,March,April,May,June,July,August,September,October,November,December")] tblSWATWAMonthlyQuantity tblswatwamonthlyquantity)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tblswatwamonthlyquantity).State = EntityState.Modified;
                db.SaveChanges();
                updateScores(tblswatwamonthlyquantity);

                // If there is not any WAannualPrecip with the current survey (SurveyID) then create one and redirecto to its edit link.
                var annualPrecips = db.tblSWATWAannualPrecips.Where(e => e.SurveyID == tblswatwamonthlyquantity.SurveyID);
                if (!annualPrecips.Any())
                {
                    tblSWATWAannualPrecip tblswatwaannualprecip = new tblSWATWAannualPrecip();
                    tblswatwaannualprecip.SurveyID = tblswatwamonthlyquantity.SurveyID;
                    db.tblSWATWAannualPrecips.Add(tblswatwaannualprecip);
                    db.SaveChanges();

                    int newWAannualPrecipID = tblswatwaannualprecip.ID;
                    return RedirectToAction("Edit", "WAannualPrecip", new { id = newWAannualPrecipID, SurveyID = tblswatwaannualprecip.SurveyID });
                }
                else
                {
                    return RedirectToAction("Edit", "WAannualPrecip", new { id = annualPrecips.Single(e => e.SurveyID == tblswatwamonthlyquantity.SurveyID).ID, SurveyID = tblswatwamonthlyquantity.SurveyID });
                }
                // return RedirectToAction("Index");
            }
            ViewBag.August = new SelectList(db.lkpSWATwaterMonthLUs, "id", "Description", tblswatwamonthlyquantity.August);
            ViewBag.September = new SelectList(db.lkpSWATwaterMonthLUs, "id", "Description", tblswatwamonthlyquantity.September);
            ViewBag.October = new SelectList(db.lkpSWATwaterMonthLUs, "id", "Description", tblswatwamonthlyquantity.October);
            ViewBag.November = new SelectList(db.lkpSWATwaterMonthLUs, "id", "Description", tblswatwamonthlyquantity.November);
            ViewBag.December = new SelectList(db.lkpSWATwaterMonthLUs, "id", "Description", tblswatwamonthlyquantity.December);
            ViewBag.January = new SelectList(db.lkpSWATwaterMonthLUs, "id", "Description", tblswatwamonthlyquantity.January);
            ViewBag.February = new SelectList(db.lkpSWATwaterMonthLUs, "id", "Description", tblswatwamonthlyquantity.February);
            ViewBag.March = new SelectList(db.lkpSWATwaterMonthLUs, "id", "Description", tblswatwamonthlyquantity.March);
            ViewBag.April = new SelectList(db.lkpSWATwaterMonthLUs, "id", "Description", tblswatwamonthlyquantity.April);
            ViewBag.May = new SelectList(db.lkpSWATwaterMonthLUs, "id", "Description", tblswatwamonthlyquantity.May);
            ViewBag.June = new SelectList(db.lkpSWATwaterMonthLUs, "id", "Description", tblswatwamonthlyquantity.June);
            ViewBag.July = new SelectList(db.lkpSWATwaterMonthLUs, "id", "Description", tblswatwamonthlyquantity.July);
              //  ViewBag.SurveyID = new SelectList(db.tblSWATSurveys, "ID", "ID", tblswatwamonthlyquantity.SurveyID);
            return View(tblswatwamonthlyquantity);
        }
        private void updateScores(tblSWATWAannualPrecip tblswatwaannualprecip)
        {
            // Update precipVarSCORE1 by giving standard deviation of annual precipitation.
            if (tblswatwaannualprecip.precipVar != null)
            {
                double? precipMean = db.tblSWATScores.Single(e => e.SurveyID == tblswatwaannualprecip.SurveyID && e.VarName == "precipTotal").Value;
                if (precipMean > 0)
                {
                    double? precipStdDivMean = tblswatwaannualprecip.precipVar.Value / precipMean;
                    var precipVardivMean = db.tblSWATScores.Single(e => e.SurveyID == tblswatwaannualprecip.SurveyID && e.VarName == "precipVardivMean");
                    precipVardivMean.Value = precipStdDivMean;
                    db.SaveChanges();
                    int? precipVardivMeanIntorder = null;

                    foreach (var item in db.lkpSWATprecipVardivMeanLUs.OrderByDescending(e => e.Description))
                    {
                        if (Double.Parse(item.Description) <= precipStdDivMean)
                        {
                            precipVardivMeanIntorder = item.intorder;
                            break;
                        }
                    }
                    if (precipVardivMeanIntorder != null)
                    {
                        double precipVarScore1Value = Double.Parse(db.lkpSWATscores_precipVar.Single(e => e.intorder == precipVardivMeanIntorder).Description);
                        var precipVarScore1 = db.tblSWATScores.Single(e => e.SurveyID == tblswatwaannualprecip.SurveyID && e.VarName == "precipVarSCORE1");
                        precipVarScore1.Value = precipVarScore1Value;
                    }
                }
                db.tblSWATScores.Single(e => e.SurveyID == tblswatwaannualprecip.SurveyID && e.VarName == "precipVarALTSCORE").Value = null;
                db.SaveChanges();
            }
            // Update precipVarALTSCORE by selecting from select list.
            else if (tblswatwaannualprecip.precipVarALT != null)
            {
                int? precipVarALT = tblswatwaannualprecip.precipVarALT;
                int? precipVarALTintorder = db.lkpSWATprecipVarAltLUs.Find(precipVarALT).intorder;
                double precipVarALTSCORE = Double.Parse(db.lkpSWATscores_precipVar.Single(e => e.intorder == precipVarALTintorder).Description);
                db.tblSWATScores.Single(e => e.SurveyID == tblswatwaannualprecip.SurveyID && e.VarName == "precipVarALTSCORE").Value = precipVarALTSCORE;
                db.tblSWATScores.Single(e => e.SurveyID == tblswatwaannualprecip.SurveyID && e.VarName == "precipVarSCORE1").Value = null;
                db.tblSWATScores.Single(e => e.SurveyID == tblswatwaannualprecip.SurveyID && e.VarName == "precipVardivMean").Value = null;
                db.SaveChanges();
            }
            else
            {
                db.tblSWATScores.Single(e => e.SurveyID == tblswatwaannualprecip.SurveyID && e.VarName == "precipVarSCORE1").Value = null;
                db.tblSWATScores.Single(e => e.SurveyID == tblswatwaannualprecip.SurveyID && e.VarName == "precipVardivMean").Value = null;
                db.tblSWATScores.Single(e => e.SurveyID == tblswatwaannualprecip.SurveyID && e.VarName == "precipVarALTSCORE").Value = null;
                db.SaveChanges();
            }
        }