public ActionResult Edit(Donor Donors) { if (ModelState.IsValid) { db.Entry(Donors).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(Donors)); }
public ActionResult DeleteAssessment(int id) { if (id != 0) { //find the assessment from the dbcontext ModelState.Clear(); BloodDonors.Models.Assessment thisAssessment = db.Assessments.Find(id); if (thisAssessment != null) { //remove subobjects first foreach (BloodDonors.Models.Borehole thisBH in thisAssessment.Boreholes) { db.BHSamples.RemoveRange(db.BHSamples.Where(x => x.BoreholdID == thisBH.BoreholeID)); db.SaveChanges(); } db.Boreholes.RemoveRange(db.Boreholes.Where(x => x.AssessmentID == thisAssessment.AssessmentID)); foreach (BloodDonors.Models.Transect thisT in thisAssessment.Transects) { db.TransectPoints.RemoveRange(db.TransectPoints.Where(x => x.TransectID == thisT.TransectId)); db.SaveChanges(); db.TransectImages.RemoveRange(db.TransectImages.Where(x => x.TransectID == thisT.TransectId)); db.SaveChanges(); } db.Transects.RemoveRange(db.Transects.Where(x => x.AssessmentID == thisAssessment.AssessmentID)); db.Assessments.Remove(thisAssessment); db.Entry(thisAssessment).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); return(Redirect(Request.UrlReferrer.ToString())); } else { return(View("Index"));; } } return(View("Index"));; }
public ActionResult Update(BloodDonors.Models.Assessment theAssessment, string theTransectBlob, string theTransectProps, string BHLocationData, string BHSampleData, string CurrentTransectName) { //ModelState.Clear(); //JavaScriptSerializer js = new JavaScriptSerializer(); //BloodDonors.Models.Assessment theAssessment = js.Deserialize<BloodDonors.Models.Assessment>(theAssessmentString); var errors = ModelState.Values.SelectMany(v => v.Errors); if (ModelState.IsValid) { try { // db.Assessments.Add(theAssment); BloodDonors.Models.Assessment DBAssessment = db.Assessments.Find(theAssessment.AssessmentID); Boolean BHChanged = false; Boolean TransectChanged = false; if (DBAssessment != null) { Type typeView = theAssessment.GetType(); System.Reflection.PropertyInfo[] propertiesView = typeView.GetProperties(); Type typeModel = DBAssessment.GetType(); System.Reflection.PropertyInfo[] propertiesModel = typeView.GetProperties(); //DBAssessment.AssessmentName = theAssessment.AssessmentName; //DBAssessment.AcceptableFoundationFailureRiskLevel = theAssessment.AcceptableFoundationFailureRiskLevel; //DBAssessment.AssessmentArea = theAssessment.AssessmentArea; foreach (System.Reflection.PropertyInfo propertyView in propertiesView) { foreach (System.Reflection.PropertyInfo PropertyModel in propertiesModel) { if (PropertyModel.Name == propertyView.Name) { string thisPropViewValue = Convert.ToString(propertyView.GetValue(theAssessment, null)); string thisPropDBValue = Convert.ToString(propertyView.GetValue(DBAssessment, null)); if (thisPropViewValue != "") { if (thisPropDBValue != thisPropViewValue) { PropertyModel.SetValue(DBAssessment, propertyView.GetValue(theAssessment, null), null); db.Entry(DBAssessment).State = System.Data.Entity.EntityState.Modified; } } } } } //transects if (theTransectBlob != null && theTransectBlob != "") { if (CurrentTransectName != null & CurrentTransectName != "") { //reconcile the transectblob with the model //delete transects that are in the model and not in the blob //add transects to the model that are in the blob but not in the model var TrasectProps = theTransectProps.Split(';'); var tPoints = theTransectBlob.Split(';'); for (int i = 0; i < TrasectProps.Length; i++) { var TP1 = TrasectProps[i].Split(','); if (TP1.Length > 1) { var TPoints1 = tPoints[i].Split(','); string theTID = TP1[0].Trim(); string theLength = TP1[1].Trim(); //find if the transect is already there Boolean TThere = false; foreach (BloodDonors.Models.Transect thisTransect in DBAssessment.Transects) { if (thisTransect.TransectName == theTID) { TThere = true; break; } } if (TThere == false) { BloodDonors.Models.Transect NewTransect = new BloodDonors.Models.Transect(); NewTransect.TransectName = theTID; NewTransect.AssessmentID = DBAssessment.AssessmentID; NewTransect.TransectPoints = new List <Models.TransectPoint>(); NewTransect.Boreholes = new List <Models.Borehole>(); DBAssessment.Transects.Add(NewTransect); db.Entry(DBAssessment).State = System.Data.Entity.EntityState.Modified; db.Entry(NewTransect).State = System.Data.Entity.EntityState.Added; TransectChanged = true; //also add the points var XY = TPoints1; for (int k = 1; k < XY.Length; k++) { BloodDonors.Models.TransectPoint NewPoint = new BloodDonors.Models.TransectPoint(); NewPoint.Longitude = Convert.ToDouble(XY[k - 1]); NewPoint.Latitude = Convert.ToDouble(XY[k]); NewPoint.PointName = Convert.ToString(k); NewTransect.TransectPoints.Add(NewPoint); //db.Entry(NewTransect).State = System.Data.Entity.EntityState.Modified; db.Entry(NewPoint).State = System.Data.Entity.EntityState.Added; k = k + 1; } //db.Transects.Add(NewTransect); } } } Boolean TransectDeleted = false; do { TransectDeleted = false; foreach (BloodDonors.Models.Transect thisTransect in DBAssessment.Transects) { for (int i = 0; i < TrasectProps.Length; i++) { if (TrasectProps[i] != "") { var TP1 = TrasectProps[i].Split(','); var TPoints1 = tPoints[i].Split(','); string theTID = TP1[0].Trim(); string theLength = TP1[1].Trim(); //find if the transect is already there Boolean TinBlob = false; if (thisTransect.TransectName == theTID) { TinBlob = true; } if (TinBlob == false) { //delete the transect from the database DBAssessment.Transects.Remove(thisTransect); db.Entry(DBAssessment).State = System.Data.Entity.EntityState.Modified; db.Entry(thisTransect).State = System.Data.Entity.EntityState.Deleted; TransectDeleted = true; break; } } } if (TransectDeleted) { break; } } } while (TransectDeleted == true); } } //delete all transects if the blob and props are empty if (theTransectBlob == "" && theTransectProps == "") { Boolean TransectDeleted = false; do { TransectDeleted = false; foreach (BloodDonors.Models.Transect thisTransect in DBAssessment.Transects) { DBAssessment.Transects.Remove(thisTransect); db.Entry(DBAssessment).State = System.Data.Entity.EntityState.Modified; db.Entry(thisTransect).State = System.Data.Entity.EntityState.Deleted; TransectDeleted = true; break; } } while (TransectDeleted == true); } //update boreholes if (BHLocationData != null && BHLocationData != "") { var BHLocations = BHLocationData.Split(';'); var BHSamples = BHSampleData.Split(';'); for (int i = 0; i < BHLocations.Length - 1; i++) { var BH1 = BHLocations[i].Split(','); var BHSamples1 = BHSamples[i].Split(','); string theBHName = BH1[0].Trim(); if (theBHName.Length > 0) { string theLongitude = BH1[2].Trim(); string theLatitude = BH1[1].Trim(); string theStation = BH1[3].Trim(); string theElevation = BH1[4].Trim(); //find if the BH is already there Boolean BHThere = false; foreach (BloodDonors.Models.Borehole thisBH in DBAssessment.Boreholes) { if (thisBH.BoreholeName == theBHName) { BHThere = true; break; } } if (BHThere == false) { BloodDonors.Models.Borehole NewBorehole = new BloodDonors.Models.Borehole(); NewBorehole.BoreholeName = theBHName; NewBorehole.AssessmentID = DBAssessment.AssessmentID; NewBorehole.Latitude = Convert.ToDouble(theLatitude); NewBorehole.Longitude = Convert.ToDouble(theLongitude); NewBorehole.BHSamples = new List <Models.BHSample>(); NewBorehole.TransectName = "Transect 1"; NewBorehole.Station = Convert.ToDouble(theStation); NewBorehole.Elevation = Convert.ToDouble(theElevation); BHChanged = true; DBAssessment.Boreholes.Add(NewBorehole); db.Entry(DBAssessment).State = System.Data.Entity.EntityState.Modified; db.Entry(NewBorehole).State = System.Data.Entity.EntityState.Added; //relate the borehole to a transect foreach (Transect thisT in DBAssessment.Transects) { if (thisT.TransectName.Trim() == CurrentTransectName.Trim()) { NewBorehole.TransectName = thisT.TransectName; if (thisT.Boreholes == null) { thisT.Boreholes = new List <BloodDonors.Models.Borehole>(); } thisT.Boreholes.Add(NewBorehole); //db.Entry(thisT).State = System.Data.Entity.EntityState.Modified; break; } } //also add the samples var lastGMC = ""; for (int k = 0; k < BHSamples.Length; k++) { var thisSample = BHSamples[k].Split(','); if (thisSample.Length >= 4) { string thisSampleName = thisSample[0].Trim(); if (thisSampleName == NewBorehole.BoreholeName) { BloodDonors.Models.BHSample NewSample = new BloodDonors.Models.BHSample(); NewSample.BoreholdID = NewBorehole.BoreholeID; NewSample.Depth = Convert.ToDouble(thisSample[1]); NewSample.Material = thisSample[2]; NewSample.PF_code = thisSample[3]; if (thisSample.Length >= 5) { if (thisSample[4] != "") { if (thisSample[4] == "ICE") { NewSample.GMC = 1000; } else { NewSample.GMC = Convert.ToDouble(thisSample[4]); } lastGMC = Convert.ToString(NewSample.GMC); } else if (lastGMC != "") { NewSample.GMC = Convert.ToDouble(lastGMC); } else { NewSample.GMC = 0; } } else { NewSample.GMC = 0; } NewBorehole.BHSamples.Add(NewSample); db.Entry(NewSample).State = System.Data.Entity.EntityState.Added; } } } //db.Transects.Add(NewTransect); } } } } EvaluateStability(DBAssessment); DBAssessment.LastModifiedDate = DateTime.Now; db.SaveChanges(); if (BHChanged || TransectChanged) { //return RedirectToAction("Open", new { id = DBAssessment.AssessmentID }); return(Json(DBAssessment, JsonRequestBehavior.AllowGet)); } else { //return null; return(Json(DBAssessment, JsonRequestBehavior.AllowGet)); } } else { return(null); } //if (DBAssessment != null) //{ // db.SaveChanges(); //} //ModelStateDictionary state = ModelState; //ModelState statevalue; //string AttemptValue = ""; //if (state.TryGetValue("AssessmentName", out statevalue)) //{ // AttemptValue = statevalue.Value.AttemptedValue.ToString(); //} ////foreach (Assessment thisassessemnt in db.Assessments) ////{ //// if (thisassessemnt.AssessmentID == theAssessment.AssessmentID ) //// { //// thisassessemnt.AssessmentName = theAssessment.AssessmentName; //// } ////} //db.SaveChanges(); //// return RedirectToAction("Index"); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } return(null); } } //return View("Complete", theAssment); return(null); }