示例#1
0
        public ActionResult EditDistributorEmployee(DistributorEmployee model, string postAction = "", int selectedAddressState = -1, List <int> selectedMunicipalities = null, List <int> selectedCrops = null)
        {
            if (model == null)
            {
                return(RedirectToError(messageDebug: controllerTraceId + "DistributorCreate[Post].111 No model was received"));
            }

            DistributorController distributorController = new DistributorController(Configurations);

            if (TempData["CropsXMunicipality"] != null)
            {
                model.Distributor.CropsXMunicipality = (ICollection <DistributorCropsXMunicipality>)TempData["CropsXMunicipality"];
                TempData["CropsXMunicipality"]       = model.Distributor.CropsXMunicipality;
            }

            List <AddressMunicipality> auxMunicipalities;

            switch (postAction)
            {
            case "getMunicipalities":     //get municipalities for the selected addressState
                auxMunicipalities = distributorController.GetMunicipalitiesByState(selectedAddressState);
                //ViewBag.SelectedPostalCode = auxPostalCode;
                ViewBag.AvailableMunicipalities = auxMunicipalities;
                ViewBag.AddressStates           = AddressStates(selectedAddressState);
                return(View(model));

            case "addInfluence":     //add "area de influencia" to the list
                auxMunicipalities = distributorController.GetMunicipalitiesByState(selectedAddressState);
                foreach (int itemMunicipalityId in selectedMunicipalities)
                {
                    foreach (int itemCropId in selectedCrops)
                    {
                        AddressMunicipality municipalityName = new AddressMunicipality();
                        foreach (AddressMunicipality aMName in auxMunicipalities)
                        {
                            if (aMName.Id == itemMunicipalityId)
                            {
                                municipalityName = aMName;
                                break;
                            }
                        }

                        model.Distributor.CropsXMunicipality.Add(new DistributorCropsXMunicipality()
                        {
                            AddressMunicipalityAddressStateId = selectedAddressState
                            ,
                            Crop = distributorController.GetCropById(itemCropId)
                            ,
                            AddressMunicipalityId = itemMunicipalityId
                            ,
                            Municipality = municipalityName
                            ,
                            Cat_CropId = itemCropId
                            ,
                            DistributorId = model.DistributorId
                        });
                        TempData["CropsXMunicipality"] = model.Distributor.CropsXMunicipality;
                    }
                }
                ViewBag.AddressStates = AddressStates();
                return(View(model));

            case "updateDistributor":
                if (controller.UpdateDistributorEmployee(model) && controller.ResultManager.IsCorrect)
                {
                    NotifyUser(messageOk: controller.ResultManager.ToDescriptionString(), messageDebug: controllerTraceId + "EmployeeCreate.831 employee created", resultDetails: controller.ErrorManager.ToStringList());
                    return(RedirectToAction("Index"));
                }
                break;

            default:
                break;
            }
            ViewBag.AddressStates = AddressStates();
            NotifyUser(messageError: controller.ResultManager.ToDescriptionString(), messageDebug: controllerTraceId + "EmployeeCreate.811", resultDetails: controller.ErrorManager.ToStringList());
            return(View(model));
        }