public void updateScores( tblSWATWAextremeEvent tblswatwaextremeevent) { int? extremeDryId = tblswatwaextremeevent.extremeDry; if (extremeDryId != null) { int? extremeDryIntorder = db.lkpSWATextremeEventsLUs.Find(extremeDryId).intorder; double? extremeDryScore = Double.Parse(db.lkpSWATscores_extremeEvents.Single(e => e.intorder == extremeDryIntorder).Description); db.tblSWATScores.Single(e => e.SurveyID == tblswatwaextremeevent.SurveyID && e.VarName == "extremeDrySCORE").Value = extremeDryScore; } else { db.tblSWATScores.Single(e => e.SurveyID == tblswatwaextremeevent.SurveyID && e.VarName == "extremeDrySCORE").Value = null; } int? extremeFloodId = tblswatwaextremeevent.extremeFlood; if (extremeFloodId != null) { int? extremeFloodIntorder = db.lkpSWATextremeEventsLUs.Find(extremeFloodId).intorder; double? extremeFloodScore = Double.Parse(db.lkpSWATscores_extremeEvents.Single(e => e.intorder == extremeFloodIntorder).Description); db.tblSWATScores.Single(e => e.SurveyID == tblswatwaextremeevent.SurveyID && e.VarName == "extremeFloodSCORE").Value = extremeFloodScore; } else { db.tblSWATScores.Single(e => e.SurveyID == tblswatwaextremeevent.SurveyID && e.VarName == "extremeFloodSCORE").Value = null; } int? extremeOtherId = tblswatwaextremeevent.extremeOther; if (extremeOtherId != null) { int? extremeOtherIntorder = db.lkpSWATextremeEventsLUs.Find(extremeOtherId).intorder; double? extremeOtherScore = Double.Parse(db.lkpSWATscores_extremeEvents.Single(e => e.intorder == extremeOtherIntorder).Description); db.tblSWATScores.Single(e => e.SurveyID == tblswatwaextremeevent.SurveyID && e.VarName == "extremeOtherSCORE").Value = extremeOtherScore; } else { db.tblSWATScores.Single(e => e.SurveyID == tblswatwaextremeevent.SurveyID && e.VarName == "extremeOtherSCORE").Value = null; } db.SaveChanges(); }
public ActionResult Edit([Bind(Include="ID,SurveyID,climateDryer,climateWetter,climateColder,climateHotter,climateSeasons")] tblSWATWAclimateChange tblswatwaclimatechange) { if (ModelState.IsValid) { db.Entry(tblswatwaclimatechange).State = EntityState.Modified; db.SaveChanges(); updateScores(tblswatwaclimatechange); // If there is not any WAExtremeEvent with the current survey (SurveyID) then create one and redirect to its edit link. var extremeEvents = db.tblSWATWAextremeEvents.Where(e => e.SurveyID == tblswatwaclimatechange.SurveyID); if (!extremeEvents.Any()) { tblSWATWAextremeEvent tblswatwaextremeevent = new tblSWATWAextremeEvent(); tblswatwaextremeevent.SurveyID = tblswatwaclimatechange.SurveyID; db.tblSWATWAextremeEvents.Add(tblswatwaextremeevent); db.SaveChanges(); int newExtremeEventID = tblswatwaextremeevent.ID; return RedirectToAction("Edit", "WAExtremeEvent", new { id = newExtremeEventID, SurveyID = tblswatwaextremeevent.SurveyID }); } else { return RedirectToAction("Edit", "WAExtremeEvent", new { id = extremeEvents.Single(e => e.SurveyID == tblswatwaclimatechange.SurveyID).ID, SurveyID = tblswatwaclimatechange.SurveyID }); } //return RedirectToAction("Index"); } return View(tblswatwaclimatechange); }