public void UpdateInitiativeFacility(InitiativeContext context,
                                             string[] selectedFacility, Initiative initiativeToUpdate)
        {
            if (selectedFacility == null)
            {
                initiativeToUpdate.InitiativeFacility = new List <InitiativeFacility>();
                return;
            }

            var selectedFacilityHS  = new HashSet <string>(selectedFacility);
            var initiativeFacilitys = new HashSet <int>
                                          (initiativeToUpdate.InitiativeFacility.Select(c => c.Facility.FacilityId));

            foreach (var facility in context.Facility)
            {
                // This is the add a meta tag condition from UI
                if (selectedFacilityHS.Contains(facility.FacilityId.ToString()))
                {
                    if (!initiativeFacilitys.Contains(facility.FacilityId))
                    {
                        initiativeToUpdate.InitiativeFacility.Add
                        (
                            new InitiativeFacility
                        {
                            InitiativeId = initiativeToUpdate.InitiativeId,
                            FacilityId   = facility.FacilityId
                        });
                    }
                }
                else
                {
                    //This is the delete a meta tag conditon
                    if (initiativeFacilitys.Contains(facility.FacilityId))
                    {
                        InitiativeFacility facilityToRemove
                            = initiativeToUpdate
                              .InitiativeFacility
                              .SingleOrDefault(i => i.FacilityId == facility.FacilityId);
                        context.Remove(facilityToRemove);
                    }
                }
            }
        }
Exemplo n.º 2
0
        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");*/
        }