public ActionResult Edit([Bind(Include="ID,SurveyID,precipVar,precipVarALT")] tblSWATWAannualPrecip tblswatwaannualprecip) { if (ModelState.IsValid) { db.Entry(tblswatwaannualprecip).State = EntityState.Modified; db.SaveChanges(); updateScores(tblswatwaannualprecip); // If there is not any WAClimateChange with the current survey (SurveyID) then create one and redirecto to its edit link. var climateChanges = db.tblSWATWAclimateChanges.Where(e => e.SurveyID == tblswatwaannualprecip.SurveyID); if (!climateChanges.Any()) { tblSWATWAclimateChange tblswatwaclimatechange = new tblSWATWAclimateChange(); tblswatwaclimatechange.SurveyID = tblswatwaannualprecip.SurveyID; db.tblSWATWAclimateChanges.Add(tblswatwaclimatechange); db.SaveChanges(); int newClimateChangeID = tblswatwaclimatechange.ID; return RedirectToAction("Edit", "WAClimateChange", new { id = newClimateChangeID, SurveyID = tblswatwaclimatechange.SurveyID}); } else { return RedirectToAction("Edit", "WAClimateChange", new { id = climateChanges.Single(e => e.SurveyID == tblswatwaannualprecip.SurveyID).ID, SurveyID = tblswatwaannualprecip.SurveyID }); } //return RedirectToAction("Index"); } ViewBag.precipVarALT = new SelectList(db.lkpSWATprecipVarAltLUs, "id", "Description", tblswatwaannualprecip.precipVarALT); ViewBag.SurveyID = new SelectList(db.tblSWATSurveys, "ID", "ID", tblswatwaannualprecip.SurveyID); return View(tblswatwaannualprecip); }
private void updateScores(tblSWATWAclimateChange tblswatwaclimatechange) { double climateDiffScore = 0; double climateChangeTrues = 0; bool[] climateChanges = {tblswatwaclimatechange.climateDryer, tblswatwaclimatechange.climateWetter, tblswatwaclimatechange.climateColder, tblswatwaclimatechange.climateHotter, tblswatwaclimatechange.climateSeasons}; foreach (bool climateChange in climateChanges) { if (climateChange) { climateChangeTrues++; } } climateDiffScore = climateChangeTrues / 5.0; db.tblSWATScores.Single(e => e.SurveyID == tblswatwaclimatechange.SurveyID && e.VarName == "climateDiffSCORE").Value = climateDiffScore; db.SaveChanges(); }