private void updateScores(tblSWATWAPrecipitation tblswatwaprecipitation) { double? [] precips = {tblswatwaprecipitation.January, tblswatwaprecipitation.February, tblswatwaprecipitation.March, tblswatwaprecipitation.April, tblswatwaprecipitation.May, tblswatwaprecipitation.June, tblswatwaprecipitation.July, tblswatwaprecipitation.August, tblswatwaprecipitation.September, tblswatwaprecipitation.October, tblswatwaprecipitation.November, tblswatwaprecipitation.December}; double? precipTotal = null; foreach (double? precip in precips) { if (precip != null) { precipTotal = precipTotal.GetValueOrDefault(0) + precip; } } db.tblSWATScores.Single(e => e.SurveyID == tblswatwaprecipitation.SurveyID && e.VarName == "precipTotal").Value = precipTotal; if (precipTotal != null) { int? precipScoreIntorder = null; foreach (var item in db.lkpSWATprecipLUs.OrderByDescending(e => e.Description)) { if (Double.Parse(item.Description) <= precipTotal) { precipScoreIntorder = item.intorder; break; } } double? precipScore = Double.Parse(db.lkpSWATscores_precip.Single(e => e.intorder == precipScoreIntorder).Description); var tblswatscore = db.tblSWATScores.Single(e => e.SurveyID == tblswatwaprecipitation.SurveyID && e.VarName == "precipSCORE"); tblswatscore.Value = precipScore; } else { db.tblSWATScores.Single(e => e.SurveyID == tblswatwaprecipitation.SurveyID && e.VarName == "precipSCORE").Value = null; } db.SaveChanges(); }
public ActionResult Edit([Bind(Include="ID,SurveyID,ClimateID,SoilID,EcoregionID,WatershedID,AridityID,UrbanDistanceID,Population,numHouseholds,numChildren,PeoplePerHH,isEconAg,isEconLs,isEconDev,isEconPris,Area,AreaForest,AreaAg,AreaInf,AreaSw,AreaWet,AreaNat,AreaProtID,AreaBmID,PriorQuality,PriorQuan,PriorSeasonal,PriorPolitics,PriorHealth,PriorFinances,PriorAccessible,PriorEquity")] tblSWATBackgroundinfo tblswatbackgroundinfo) { // Check if the sum of Forest(%), Agriculture(%), Infrastructure(%), Source Water(%) and Wetlands(%) exceeds 100. var totalArea = tblswatbackgroundinfo.AreaForest.GetValueOrDefault(0) + tblswatbackgroundinfo.AreaAg.GetValueOrDefault(0) + tblswatbackgroundinfo.AreaInf.GetValueOrDefault(0) + tblswatbackgroundinfo.AreaSw.GetValueOrDefault(0) + tblswatbackgroundinfo.AreaWet.GetValueOrDefault(0); if (totalArea > 100) { ModelState.AddModelError("AreaForest", "The sum of Forest(%), Agriculture(%), Infrastructure(%), Source Water(%) and Wetlands(%) cannot exceed 100."); ModelState.AddModelError("AreaAg", "The sum of Forest(%), Agriculture(%), Infrastructure(%), Source Water(%) and Wetlands(%) cannot exceed 100."); ModelState.AddModelError("AreaInf", "The sum of Forest(%), Agriculture(%), Infrastructure(%), Source Water(%) and Wetlands(%) cannot exceed 100."); ModelState.AddModelError("AreaSw", "The sum of Forest(%), Agriculture(%), Infrastructure(%), Source Water(%) and Wetlands(%) cannot exceed 100."); ModelState.AddModelError("AreaWet", "The sum of Forest(%), Agriculture(%), Infrastructure(%), Source Water(%) and Wetlands(%) cannot exceed 100."); } if (ModelState.IsValid) { db.Entry(tblswatbackgroundinfo).State = EntityState.Modified; db.SaveChanges(); updateScores(tblswatbackgroundinfo); // If there is not a WAPreciptation with the current survey (SurveyID) then create one and redirecto to its edit link. var waprecip = db.tblSWATWAPrecipitations.Where(e => e.SurveyID == tblswatbackgroundinfo.SurveyID); if (!waprecip.Any()) { tblSWATWAPrecipitation tblswatwaprecipitation = new tblSWATWAPrecipitation(); tblswatwaprecipitation.SurveyID = tblswatbackgroundinfo.SurveyID; db.tblSWATWAPrecipitations.Add(tblswatwaprecipitation); db.SaveChanges(); var newWAPreciptationID = tblswatwaprecipitation.ID; return RedirectToAction("Edit", "WAPrecipitation", new { id = tblswatwaprecipitation.ID, SurveyID = tblswatwaprecipitation.SurveyID }); } else { return RedirectToAction("Edit", "WAPrecipitation", new { id = waprecip.Single(e => e.SurveyID == tblswatbackgroundinfo.SurveyID).ID, SurveyID = tblswatbackgroundinfo.SurveyID }); } //return RedirectToAction("Index"); } ViewBag.EcoregionID = new SelectList(db.lkpBiomes, "ID", "Description", tblswatbackgroundinfo.EcoregionID); ViewBag.ClimateID = new SelectList(db.lkpClimateClassifications, "ID", "CCType", tblswatbackgroundinfo.ClimateID); ViewBag.SoilID = new SelectList(db.lkpSoils, "ID", "Name", tblswatbackgroundinfo.SoilID); ViewBag.AreaProtID = new SelectList(db.lkpSWATareaProtLUs, "id", "Description", tblswatbackgroundinfo.AreaProtID); ViewBag.AridityID = new SelectList(db.lkpSWATmapAridities, "id", "Description", tblswatbackgroundinfo.AridityID); ViewBag.UrbanDistanceID = new SelectList(db.lkpSWATurbanDistanceLUs, "id", "Description", tblswatbackgroundinfo.UrbanDistanceID); ViewBag.WatershedID = new SelectList(db.lkpSWATWatershedsLUs, "ID", "Description", tblswatbackgroundinfo.WatershedID); //ViewBag.SurveyID = new SelectList(db.tblSWATSurveys, "ID", "ID", tblswatbackgroundinfo.SurveyID); ViewBag.AreaBmID = new SelectList(db.lkpSWATareaBMLUs, "id", "Description", tblswatbackgroundinfo.AreaBmID); ViewBag.isEconPris = new SelectList(db.lkpSWATeconPrisLUs, "id", "Description", tblswatbackgroundinfo.isEconPris); ViewBag.PriorQuality = new SelectList(db.lkpSWATpriorLUs, "id", "Description", tblswatbackgroundinfo.PriorQuality); ViewBag.PriorQuan = new SelectList(db.lkpSWATpriorLUs, "id", "Description", tblswatbackgroundinfo.PriorQuan); ViewBag.PriorSeasonal = new SelectList(db.lkpSWATpriorLUs, "id", "Description", tblswatbackgroundinfo.PriorSeasonal); ViewBag.PriorPolitics = new SelectList(db.lkpSWATpriorLUs, "id", "Description", tblswatbackgroundinfo.PriorPolitics); ViewBag.PriorHealth = new SelectList(db.lkpSWATpriorLUs, "id", "Description", tblswatbackgroundinfo.PriorHealth); ViewBag.PriorFinances = new SelectList(db.lkpSWATpriorLUs, "id", "Description", tblswatbackgroundinfo.PriorFinances); ViewBag.PriorAccessible = new SelectList(db.lkpSWATpriorLUs, "id", "Description", tblswatbackgroundinfo.PriorAccessible); ViewBag.PriorEquity = new SelectList(db.lkpSWATpriorLUs, "id", "Description", tblswatbackgroundinfo.PriorEquity); ViewBag.isEconAg = new SelectList(db.lkpSWATYesNoLUs, "id", "Description", tblswatbackgroundinfo.isEconAg); ViewBag.isEconLs = new SelectList(db.lkpSWATYesNoLUs, "id", "Description", tblswatbackgroundinfo.isEconLs); ViewBag.isEconDev = new SelectList(db.lkpSWATYesNoLUs, "id", "Description", tblswatbackgroundinfo.isEconDev); ViewBag.SurveyID = tblswatbackgroundinfo.SurveyID; return View(tblswatbackgroundinfo); }