Пример #1
0
        public async Task <IActionResult> CreateFriend(PostViewModel vm)
        {
            ResultModel Cityresult = new ResultModel();
            //下載cityList
            var downloadCityList = new DownloadCityList(_config, _callApi);

            vm.CityList = await downloadCityList.DownloadToList();

            var cityObj = await downloadCityList.Download();

            if (vm.friend.CityId != null)
            {
                var selectCity = cityObj.SingleOrDefault(c => c.Id == vm.friend.CityId);
                vm.TownShipList = from o in selectCity.TownShips
                                  select new SelectListItem
                {
                    Value = o.Id.ToString(),
                    Text  = o.Name
                };
                if (vm.friend.TownshipId != null)
                {
                    var selectTownShip = selectCity.TownShips.SingleOrDefault(t => t.Id == vm.friend.TownshipId);
                    vm.friend.Zip = selectTownShip.Zip;
                }
            }
            //END下載cityList
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            var         data    = JsonConvert.SerializeObject(vm.friend);
            Uri         address = new Uri(_config["api"] + "/Friends");
            ResultModel result  = new ResultModel();

            result = await _callApi.CallAPI(data, address, "POST");

            TempData["IsSuccess"] = result.IsSuccess;
            TempData["msg"]       = result.Message;
            if (!result.IsSuccess)
            {
                return(View(vm));
            }
            return(RedirectToAction(nameof(CreateFriend)));
        }
Пример #2
0
        public async Task <IActionResult> EditFriend(int id)
        {
            ResultModel result  = new ResultModel();
            Uri         address = new Uri(_config["api"] + "/Friends/" + id);

            result = await _callApi.CallAPI("null", address, "GET");


            PostViewModel vm = new PostViewModel();

            if (result.IsSuccess == true)
            {
                vm.friend             = JsonConvert.DeserializeObject <Friend>(result.Data.ToString());
                TempData["isSuccess"] = "true";

                if (result.IsSuccess)//下載cityList
                {
                    ResultModel Cityresult = new ResultModel();
                    //下載cityList
                    var downloadCityList = new DownloadCityList(_config, _callApi);
                    vm.CityList = await downloadCityList.DownloadToList();

                    var cityObj = await downloadCityList.Download();

                    var selectCity     = cityObj.SingleOrDefault(c => c.Id == vm.friend.CityId);
                    var selectTownShip = selectCity.TownShips.SingleOrDefault(t => t.Id == vm.friend.TownshipId);
                    vm.friend.Zip   = selectTownShip.Zip;
                    vm.TownShipList = from o in selectCity.TownShips
                                      select new SelectListItem
                    {
                        Value = o.Id.ToString(),
                        Text  = o.Name
                    };
                }

                return(View(vm));
            }
            else
            { //result.IsSuccess == false
                TempData["isSuccess"] = "false";
                TempData["msg"]       = result.Message;
                return(View());
            }
        }