public ActionResult UpdateSummaryReportTab(ProjectVM model, HttpPostedFileBase File) { try { ///////////////////////////AOI/////////////////////////////////////////////////////// AreaOFInterest areaOFInterest = new AreaOFInterest(); if (model.AOIList != null) { if (model.AOIList.Count > 0) { // retrive all existing aoi of this project delete them then add all new aoi List <ProjectsAOI> pAOI = (from s in db.ProjectsAOIs where s.SPtId == model.SPtId select s).ToList(); if (pAOI != null) { for (int item = 0; item < pAOI.Count; item++) { //Delete them from ProjectsAOI table ProjectsAOI paoi = db.ProjectsAOIs.Find(pAOI[item].Id); // new ProjectsAOI { Id = pAOI[item].Id }; db.Entry(paoi).State = EntityState.Deleted; db.SaveChanges(); } } // Now I will add every AOI again to mapping table ProjectsAOI projectAOI = new ProjectsAOI(); for (int i = 0; i < model.AOIList.Count; i++) { string AOIName = model.AOIList.ElementAt(i).Trim().ToUpper(); var temp = (from s in db.AreaOFInterests where s.AOIName.ToUpper() == AOIName select s).FirstOrDefault(); //means it exists in AreaOfInterest table if (AOIName != "") { if (temp != null) { //get area id projectAOI.SPtId = model.SPtId; projectAOI.AOIId = temp.AOIId; } // else means no aoi has this name I will add it to area table then to mapping table if (temp == null) { AreaOFInterest newAOI = new AreaOFInterest(); newAOI.AOIName = model.AOIList.ElementAt(i).Trim(); using (Entities entity = new Entities()) { entity.AreaOFInterests.Add(newAOI); entity.SaveChanges(); } // get last added aoi then add it to aoi mapping table AreaOFInterest LastArea = db.AreaOFInterests.OrderByDescending(o => o.AOIId).FirstOrDefault(); projectAOI.SPtId = model.SPtId; projectAOI.AOIId = LastArea.AOIId; } using (Entities entity = new Entities()) { entity.ProjectsAOIs.Add(projectAOI); entity.SaveChanges(); } } } } } ///////////////////////////Awards/////////////////////////////////////////////////////// if (model.AwardsList != null) { if (model.AwardsList.Count > 0) { // retrive all existing awards of this project delete them then add all new awards List <ProjectAward> pAward = (from s in db.ProjectAwards where s.ProjectId == model.SPtId select s).ToList(); if (pAward != null) { foreach (var item in pAward) { //delete them from ProjectAward table ProjectAward PAward = db.ProjectAwards.Find(item.Id); db.Entry(PAward).State = EntityState.Deleted; db.SaveChanges(); //delete them from Award table Award Award = db.Awards.Find(item.AwardId); var itemm = item.AwardId; db.Entry(Award).State = EntityState.Deleted; db.SaveChanges(); } } // Now I will add every award again to award table and mapping table for (int i = 0; i < model.AwardsList.Count; i++) { Award award = new Award(); award.awardName = model.AwardsList.ElementAt(i).Trim(); using (Entities entity = new Entities()) { entity.Awards.Add(award); entity.SaveChanges(); } //get last added award id Award LastAward = db.Awards.OrderByDescending(o => o.awardId).FirstOrDefault(); ProjectAward projectAward = new ProjectAward(); projectAward.AwardId = LastAward.awardId; projectAward.ProjectId = model.SPtId; using (Entities entity = new Entities()) { entity.ProjectAwards.Add(projectAward); entity.SaveChanges(); } } } } ///////////////programming languages////////////////////////////////////////////////// string PL = ""; //set programming languages in one string if (model.PLList != null) { if (model.PLList.Count > 0) { for (int i = 0; i < model.PLList.Count - 1; i++) { if (model.PLList.ElementAt(i).Trim() != "") { PL += model.PLList.ElementAt(i).Trim().ToLower() + ","; } } if (model.PLList.ElementAt(model.PLList.Count - 1).Trim() != "") { PL += model.PLList.ElementAt(model.PLList.Count - 1).Trim().ToLower(); } } } ProjectEdit(model.SPtId, model.SPName, model.SPVideos, model.SPAbstract, model.SPGrade, PL, model.Section, model.Year, File); return(new JsonResult { Data = "Saved successfully" }); } catch (Exception ex) { return(new JsonResult { Data = "an error occurred " + ex.Message }); } }
public ActionResult SaveReport(ProjectVM model) { try { AreaOFInterest aoi = new AreaOFInterest(); ProjectsAOI paoi = new ProjectsAOI(); List <int> Ids = new List <int>(); if (model.AOIList != null) { if (model.AOIList.Count > 0) { for (var i = 0; i < model.AOIList.Count; i++) { var item = model.AOIList.ElementAt(i).Trim().ToUpper(); var area = from s in db.AreaOFInterests where s.AOIName.ToUpper() == item select s; //if area not found it will be added to the table if (!area.Any()) { aoi.AOIName = model.AOIList.ElementAt(i).Trim(); using (Entities entity = new Entities()) { entity.AreaOFInterests.Add(aoi); entity.SaveChanges(); } } } for (var i = 0; i < model.AOIList.Count; i++) { var item = model.AOIList.ElementAt(i).Trim().ToUpper(); int areaId = (from FC in db.AreaOFInterests where FC.AOIName.ToUpper() == item select FC.AOIId).ToList().FirstOrDefault(); //assigen area to tables if (areaId != 0) { paoi.AOIId = areaId; paoi.SPtId = model.SPtId; using (Entities entity = new Entities()) { entity.ProjectsAOIs.Add(paoi); entity.SaveChanges(); } } } } } Award award = new Award(); if (model.AwardsList != null) { if (model.AwardsList.Count > 0) { for (int i = 0; i < model.AwardsList.Count; i++) { award.awardName = model.AwardsList.ElementAt(i).Trim(); using (Entities entity = new Entities()) { entity.Awards.Add(award); entity.SaveChanges(); } } /*********************************Add awards to projectAward table (to mapping between project and award)*******************************************************************/ ProjectAward projectAward = new ProjectAward(); for (int i = 0; i < model.AwardsList.Count; i++) { var item = model.AwardsList.ElementAt(i).Trim(); //get IDs of each award int?awardId = (from FC in db.Awards where FC.awardName == item select FC.awardId).ToList().FirstOrDefault(); projectAward.ProjectId = model.SPtId; projectAward.AwardId = (int)awardId; //assigen awards to mapping tables if (awardId != null) { using (Entities entity = new Entities()) { entity.ProjectAwards.Add(projectAward); entity.SaveChanges(); } } } } } string PL = ""; //set programming languages in one string if (model.PLList != null) { if (model.PLList.Count > 0) { for (int i = 0; i < model.PLList.Count - 1; i++) { PL += model.PLList.ElementAt(i).Trim().ToLower() + ","; } PL += model.PLList.ElementAt(model.PLList.Count - 1).Trim().ToLower(); } } ProjectEdit(model.SPtId, model.SPName, model.SPVideos, model.SPAbstract, model.SPGrade, PL); return(new JsonResult { Data = "Saved successfully" }); } catch (Exception ex) { return(new JsonResult { Data = "an error occurred " + ex.Message }); } }