public void UpdateInitiativeMetaTags(InitiativeContext context, string[] selectedMetaTags, Initiative initiativeToUpdate) { if (selectedMetaTags == null) { initiativeToUpdate.InitiativeMetaTag = new List <InitiativeMetaTag>(); return; } var selectedMetaTagsHS = new HashSet <string>(selectedMetaTags); var initiativeMetaTags = new HashSet <int> (initiativeToUpdate.InitiativeMetaTag.Select(c => c.MetaTag.MetaTagId)); foreach (var metaTag in context.MetaTag) { // This is the add a meta tag condition from UI if (selectedMetaTagsHS.Contains(metaTag.MetaTagId.ToString())) { if (!initiativeMetaTags.Contains(metaTag.MetaTagId)) { initiativeToUpdate.InitiativeMetaTag.Add ( new InitiativeMetaTag { InitiativeId = initiativeToUpdate.InitiativeId, MetaTagId = metaTag.MetaTagId }); } } else { //This is the delete a meta tag conditon if (initiativeMetaTags.Contains(metaTag.MetaTagId)) { InitiativeMetaTag metaTagToRemove = initiativeToUpdate .InitiativeMetaTag .SingleOrDefault(i => i.MetaTagId == metaTag.MetaTagId); context.Remove(metaTagToRemove); } } } }
public async Task <IActionResult> OnPostAsync(string[] selectedBusiness, string[] selectedFacility, string[] selectedMetaTags) { if (!ModelState.IsValid) { return(Page()); } /*******************************************/ //Get the entity var newInitiative = new Initiative(); // Add Business if (selectedBusiness != null) { newInitiative.InitiativeBusiness = new List <InitiativeBusiness>(); foreach (var business in selectedBusiness) { var initiativeBusinessToAdd = new InitiativeBusiness { BusinessId = int.Parse(business) }; newInitiative.InitiativeBusiness.Add(initiativeBusinessToAdd); } } // Add Facility if (selectedFacility != null) { newInitiative.InitiativeFacility = new List <InitiativeFacility>(); foreach (var facility in selectedFacility) { var initiativeFacilityToAdd = new InitiativeFacility { FacilityId = int.Parse(facility) }; newInitiative.InitiativeFacility.Add(initiativeFacilityToAdd); } } // Add Meta Tags if (selectedMetaTags != null) { newInitiative.InitiativeMetaTag = new List <InitiativeMetaTag>(); foreach (var metaTag in selectedMetaTags) { var initiativeMetaTagToAdd = new InitiativeMetaTag { MetaTagId = int.Parse(metaTag) }; newInitiative.InitiativeMetaTag.Add(initiativeMetaTagToAdd); } } // Add Facility //the method of updating below is used as a security measure and to prevent over posting if (await TryUpdateModelAsync <Initiative>( newInitiative, "Initiative", i => i.ARBDate, i => i.EngagementName, i => i.EngagementIdentifier, i => i.EngagementTypeId, i => i.CurrentStatusId, i => i.SolutionTypeId, i => i.LocationId, i => i.PHI, i => i.PCI, i => i.UpStreamSystem, i => i.DownStreamSystem, i => i.ProjectStartDate, i => i.Resource, i => i.ReceiveDate, i => i.StartDate, i => i.CompleteDate, i => i.LastModifiedDate, i => i.ModifiedUserName, i => i.IsActive)) { _context.Initiative.Add(newInitiative); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } PopulateAssignedMetaTagData(_context, newInitiative); return(Page()); /*************************************** * _context.Initiative.Add(Initiative); * await _context.SaveChangesAsync(); * * return RedirectToPage("./Index");*/ }