public IActionResult ShowForm(int?Id)
        {
            FamilyResearchViewModel model = new FamilyResearchViewModel();

            model.LoadMaritalStatuses(familyResearchRepository);
            model.LoadRelationships(familyResearchRepository);
            model.LoadStatesOfMexico(familyResearchRepository);
            model.LoadMunicipalitiesOfMexico(familyResearchRepository);
            model.LoadHomeAcquisitionList(familyResearchRepository);
            model.LoadTipoDeMobiliarioList(familyResearchRepository);
            model.LoadTypeOfDistrictList(familyResearchRepository);
            model.SetInitialPatrimonyViewModelCollection(familyResearchRepository);
            model.LoadFoods(familyResearchRepository);
            model.LoadFrequencies(familyResearchRepository);
            model.LoadTypesOfHousesList(familyResearchRepository);
            model.VisitDate     = DateTime.Now.ToCentralMexicoTime();
            model.FormVisitTime = DateTime.Now.ToCentralMexicoTime().ToShortTimeString();
            model.CreationDate  = DateTime.Now.ToCentralMexicoTime();

            if (Id > 0)
            {
                FamilyResearch familyResearch = familyResearchRepository.GetFamilyResearchById((int)Id);
                model.Id = familyResearch.Id;
                model.CaseStudyConclusion = familyResearch.CaseStudyConclusion;
                model.District            = familyResearch.District;
                model.DistrictId          = familyResearch.DistrictId;
                model.EconomicSituationId = familyResearch.EconomicSituationId;
                model.Family             = familyResearch.Family;
                model.FamilyDiagnostic   = familyResearch.FamilyDiagnostic;
                model.FamilyExpectations = familyResearch.FamilyExpectations;
                model.FamilyHealth       = familyResearch.FamilyHealth;
                model.FamilyHealthId     = familyResearch.FamilyHealthId;
                model.FamilyNutrition    = familyResearch.FamilyNutrition;
                model.FamilyNutritionId  = familyResearch.FamilyNutritionId;
                model.LegalGuardian      = familyResearch.LegalGuardian;
                model.LegalGuardianId    = familyResearch.LegalGuardianId;
                model.Minor   = familyResearch.Minor;
                model.MinorId = familyResearch.MinorId;
                model.Minor.FormalEducation        = familyResearch.Minor.FormalEducation;
                model.Minor.FormalEducationId      = familyResearch.Minor.FormalEducationId;
                model.PreviousFoundation           = familyResearch.PreviousFoundation;
                model.PreviousFoundationId         = familyResearch.PreviousFoundationId;
                model.ProblemsIdentified           = familyResearch.ProblemsIdentified;
                model.Recommendations              = familyResearch.Recommendations;
                model.RedesDeApoyoFamiliares       = familyResearch.RedesDeApoyoFamiliares;
                model.RequestReasons               = familyResearch.RequestReasons;
                model.SituationsOfDomesticViolence = familyResearch.SituationsOfDomesticViolence;
                model.Sketch               = familyResearch.Sketch;
                model.SocioEconomicStudy   = familyResearch.SocioEconomicStudy;
                model.SocioEconomicStudyId = familyResearch.SocioEconomicStudyId;
                model.VisualSupports       = familyResearch.VisualSupports;
                model.LoadMunicipalitiesOfMexico(familyResearchRepository);
                model.FormVisitTime    = familyResearch.VisitTime.ToShortTimeString();
                model.VisitDate        = familyResearch.VisitDate;
                model.CreationDate     = familyResearch.CreationDate;
                model.SocialWorkerName = familyResearch.SocialWorkerName;

                model.LoadFamilyNutritionFoodRelation(familyResearch.FamilyNutrition);
                model.LoadPatrimonyViewModelCollection(familyResearch.EconomicSituation);
                model.FamilyMembers   = familyResearch.FamilyMembers;
                model.FamilyMembersId = familyResearch.FamilyMembersId;
                model.SetFamilyMembersInSession(familyResearch.FamilyMembers);
                model.BenefitsProvidedList       = model.ConvertBenefitsProvidedToBenefitsProvidedViewModel(familyResearch.BenefitsProvided?.BenefitsProvidedDetails);
                model.BenefitsProvidedId         = familyResearch.BenefitsProvidedId;
                model.IngresosEgresosMensuales   = familyResearch.IngresosEgresosMensuales;
                model.IngresosMensualesList      = model.ConvertIngresosEgresosMensualesMovimientoRelationToIngresosMensualesViewModel(familyResearch.IngresosEgresosMensuales?.IngresosEgresosMensualesMovimientoRelation);
                model.IngresosEgresosMensualesId = familyResearch.IngresosEgresosMensualesId;
                model.EgresosMensualesList       = model.ConvertIngresosEgresosMensualesMovimientoRelationToEgresosMensualesViewModel(familyResearch.IngresosEgresosMensuales?.IngresosEgresosMensualesMovimientoRelation);

                return(View(model));
            }

            return(View(model));
        }
        public IActionResult AddOrUpdateFamilyResearch([FromForm] FamilyResearchViewModel model)
        {
            try
            {
                if (model == null)
                {
                    throw new ArgumentNullException(nameof(model), "Model cannot be null or empty");
                }

                if (model.IsValid(ModelState) == false)
                {
                    model.LoadMaritalStatuses(familyResearchRepository);
                    model.LoadRelationships(familyResearchRepository);
                    model.LoadStatesOfMexico(familyResearchRepository);
                    model.LoadMunicipalitiesOfMexico(familyResearchRepository);
                    model.LoadHomeAcquisitionList(familyResearchRepository);
                    model.LoadTipoDeMobiliarioList(familyResearchRepository);
                    model.LoadTypeOfDistrictList(familyResearchRepository);
                    model.SetInitialPatrimonyViewModelCollection(familyResearchRepository);
                    model.LoadFoods(familyResearchRepository);
                    model.LoadFrequencies(familyResearchRepository);
                    model.LoadTypesOfHousesList(familyResearchRepository);
                    model.CreationDate = DateTime.Now.ToCentralMexicoTime();

                    return(View("ShowForm", model));
                }

                model.VisitTime = DateTime.Parse(model.FormVisitTime);
                model.GetEconomicSituationPatrimonyRelationFromViewModel(familyResearchRepository);
                model.GetFamilyNutritionFoodRelationFromViewModel(familyResearchRepository);
                model.GetBenefitsProvidedFromSession();
                model.GetIngresosMensualesFromSession();
                model.GetFamilyMembersFromSession();

                if (model.Id > 0) // update item
                {
                    if (model.LegalGuardianId > 0)
                    {
                        model.LegalGuardian.Id = Convert.ToInt32(model.LegalGuardianId);
                    }

                    if (model.MinorId > 0)
                    {
                        model.Minor.Id = Convert.ToInt32(model.MinorId);
                    }

                    if (model.PreviousFoundationId > 0)
                    {
                        model.PreviousFoundation.Id = Convert.ToInt32(model.PreviousFoundationId);
                    }

                    if (model.FamilyHealthId > 0)
                    {
                        model.FamilyHealth.Id = Convert.ToInt32(model.FamilyHealthId);
                    }

                    if (model.SocioEconomicStudyId > 0)
                    {
                        model.SocioEconomicStudy.Id = Convert.ToInt32(model.SocioEconomicStudyId);
                    }

                    if (model.IngresosEgresosMensualesId > 0)
                    {
                        model.IngresosEgresosMensuales.Id = Convert.ToInt32(model.IngresosEgresosMensualesId);
                    }

                    if (model.DistrictId > 0)
                    {
                        model.District.Id = Convert.ToInt32(model.DistrictId);
                    }

                    if (model.Minor.FormalEducationId > 0)
                    {
                        model.Minor.FormalEducation.Id = Convert.ToInt32(model.Minor.FormalEducationId);
                    }

                    familyResearchRepository.Update(model);
                }
                else // add new item
                {
                    familyResearchRepository.Add(model);
                }
            }
            catch (BusinessLogicException ex)
            {
                ModelState.AddModelError("", ex.Message);
                model.LoadMaritalStatuses(familyResearchRepository);
                model.LoadRelationships(familyResearchRepository);
                model.LoadStatesOfMexico(familyResearchRepository);
                model.LoadMunicipalitiesOfMexico(familyResearchRepository);
                model.LoadHomeAcquisitionList(familyResearchRepository);
                model.LoadTipoDeMobiliarioList(familyResearchRepository);
                model.LoadTypeOfDistrictList(familyResearchRepository);
                model.SetInitialPatrimonyViewModelCollection(familyResearchRepository);
                model.LoadFoods(familyResearchRepository);
                model.LoadFrequencies(familyResearchRepository);
                model.LoadTypesOfHousesList(familyResearchRepository);

                return(View("ShowForm", model));
            }
            catch (Exception)
            {
                throw;
            }

            ClearSessionForTablesObject();

            return(RedirectToAction("Index"));
        }