public ActionResult Edit([Bind(Include="ID,latitude,longitude,countryID,name,regionID,subnationalID")] tblSWATLocation tblswatlocation, int? uid, int SurveyID) { if (ModelState.IsValid) { db.Entry(tblswatlocation).State = EntityState.Modified; db.SaveChanges(); var BackgrdInfo = db.tblSWATBackgroundinfoes.Where(item => item.SurveyID == SurveyID); if (!BackgrdInfo.Any()) { tblSWATBackgroundinfo tblswatbackgroundinfo = new tblSWATBackgroundinfo(); tblswatbackgroundinfo.SurveyID = SurveyID; db.tblSWATBackgroundinfoes.Add(tblswatbackgroundinfo); db.SaveChanges(); var newBackgroundInfoID = tblswatbackgroundinfo.ID; return RedirectToAction("Edit", "Background", new { id = newBackgroundInfoID, SurveyID = SurveyID }); } else { return RedirectToAction("Edit", "Background", new { id = BackgrdInfo.Select(item => item.ID).First(), SurveyID = SurveyID }); } //return RedirectToAction("Details", "User", new { id=uid}); } ViewBag.countryID = new SelectList(db.lkpCountries, "ID", "Name", tblswatlocation.countryID); ViewBag.regionID = new SelectList(db.lkpRegions, "ID", "Name", tblswatlocation.regionID); ViewBag.subnationalID = new SelectList(db.lkpSubnationals, "ID", "Name", tblswatlocation.subnationalID); return View(tblswatlocation); }
// Helper method to update the scores that occur in the background infomation section private void updateScores(tblSWATBackgroundinfo tblswatbackgroundinfo) { var sectionscorevars = db.lkpSWATSectionLUs.Include(t=>t.lkpSWATScoreVarsLUs).Single(section => section.ID == 1); foreach (var item in sectionscorevars.lkpSWATScoreVarsLUs) { if (item.VarName == "ariditySCORE") { if (tblswatbackgroundinfo.AridityID != null) { var scoreIntorder = db.lkpSWATmapAridities.Find(tblswatbackgroundinfo.AridityID).intorder; var aridityScore = db.lkpSWATscores_Aridity.Single(e => e.intorder == scoreIntorder).Description; db.tblSWATScores.Single(e => e.SurveyID == tblswatbackgroundinfo.SurveyID && e.VariableID == item.ID).Value = Double.Parse(aridityScore); } else { db.tblSWATScores.Single(e => e.SurveyID == tblswatbackgroundinfo.SurveyID && e.VariableID == item.ID).Value = null; } db.SaveChanges(); } if (item.VarName == "econPrisSCORE") { if (tblswatbackgroundinfo.isEconPris != null) { var scoreIntorder = db.lkpSWATeconPrisLUs.Find(tblswatbackgroundinfo.isEconPris).intorder; var econPrisScore = db.lkpSWATscores_econPris.Single(e => e.intorder == scoreIntorder).Description; db.tblSWATScores.Single(e => e.SurveyID == tblswatbackgroundinfo.SurveyID && e.VariableID == item.ID).Value = Double.Parse(econPrisScore); } else { db.tblSWATScores.Single(e => e.SurveyID == tblswatbackgroundinfo.SurveyID && e.VariableID == item.ID).Value = null; } db.SaveChanges(); } if (item.VarName == "areaBMSCORE") { if (tblswatbackgroundinfo.AreaBmID != null) { var scoreIntorder = db.lkpSWATareaBMLUs.Find(tblswatbackgroundinfo.AreaBmID).intorder; var areaBMScore = db.lkpSWATscores_areaBMLU.Single(e => e.intorder == scoreIntorder).Description; db.tblSWATScores.Single(e => e.SurveyID == tblswatbackgroundinfo.SurveyID && e.VariableID == item.ID).Value = Double.Parse(areaBMScore); } else { db.tblSWATScores.Single(e => e.SurveyID == tblswatbackgroundinfo.SurveyID && e.VariableID == item.ID).Value = null; } db.SaveChanges(); } if (item.VarName == "areaProtSCORE") { if (tblswatbackgroundinfo.AreaProtID != null) { var scoreIntorder = db.lkpSWATareaProtLUs.Find(tblswatbackgroundinfo.AreaProtID).intorder; var areaProtScore = db.lkpSWATscores_areaProt.Single(e => e.intorder == scoreIntorder).Description; db.tblSWATScores.Single(e => e.SurveyID == tblswatbackgroundinfo.SurveyID && e.VariableID == item.ID).Value = Double.Parse(areaProtScore); } else { db.tblSWATScores.Single(e => e.SurveyID == tblswatbackgroundinfo.SurveyID && e.VariableID == item.ID).Value = null; } db.SaveChanges(); } if (item.VarName == "urbanDistanceSCORE") { if (tblswatbackgroundinfo.UrbanDistanceID != null) { var scoreIntorder = db.lkpSWATurbanDistanceLUs.Find(tblswatbackgroundinfo.UrbanDistanceID).intorder; var urbanDistanceScore = db.lkpSWATscores_urbanDistance.Single(e => e.intorder == scoreIntorder).Description; db.tblSWATScores.Single(e => e.SurveyID == tblswatbackgroundinfo.SurveyID && e.VariableID == item.ID).Value = Double.Parse(urbanDistanceScore); } else { db.tblSWATScores.Single(e => e.SurveyID == tblswatbackgroundinfo.SurveyID && e.VariableID == item.ID).Value = null; } db.SaveChanges(); } } }