Exemplo n.º 1
0
        // GET: PersonsController/Edit/5
        public async Task <IActionResult> Edit(int id)
        {
            AddEditPersonViewModel model = new AddEditPersonViewModel();

            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.GetAsync("https://localhost:44317/api/Persons/" + id))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();

                    model = JsonConvert.DeserializeObject <AddEditPersonViewModel>(apiResponse);
                }
                using (var response = await httpClient.GetAsync("https://localhost:44317/api/Gender/GetAll"))
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        model.Gender = JsonConvert.DeserializeObject <IEnumerable <Gender> >(apiResponse);
                    }
                }

                using (var response = await httpClient.GetAsync("https://localhost:44317/api/Address/GetAll"))
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        model.Address = JsonConvert.DeserializeObject <IEnumerable <City> >(apiResponse);
                    }
                }
            }
            return(View(model));
        }
Exemplo n.º 2
0
 public AddEditPersonPage(Person pers = null, bool delete = false)
 {
     InitializeComponent();
     _addEditPersonViewModel = new AddEditPersonViewModel(Navigation, pers, delete);
     BindingContext          = _addEditPersonViewModel;
     if (delete)
     {
         Title             = "Delete person";
         SubmitButton.Text = "Delete Person";
     }
 }
Exemplo n.º 3
0
 public AddEditPersonView()
 {
     InitializeComponent();
     DataContext = new AddEditPersonViewModel();
 }
Exemplo n.º 4
0
 public AddEditPerson()
 {
     ViewModel = new AddEditPersonViewModel();
     InitializeComponent();
 }
Exemplo n.º 5
0
        public async Task <IActionResult> Edit(AddEditPersonViewModel model)
        {
            if (!string.IsNullOrEmpty(model.PhoneNumber))
            {
                model.Phones = new List <Phone>
                {
                    new Phone {
                        Number = model.PhoneNumber, PhoneTypeID = model.PhoneTypeID
                    }
                };
            }


            if (model.File != null)
            {
                using (var httpClient = new HttpClient())
                {
                    var fileName = ContentDispositionHeaderValue.Parse(model.File.ContentDisposition).FileName.Trim('"');

                    using (var content = new MultipartFormDataContent())
                    {
                        content.Add(new StreamContent(model.File.OpenReadStream())
                        {
                            Headers =
                            {
                                ContentLength = model.File.Length,
                                ContentType   = new MediaTypeHeaderValue(model.File.ContentType)
                            }
                        }, "File", fileName);


                        using (var response = await httpClient.PostAsync("https://localhost:44317/api/Persons/FileUpload", content))
                        {
                            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                            {
                                string apiResponse = await response.Content.ReadAsStringAsync();

                                var fileresult = JsonConvert.DeserializeObject <Attachment>(apiResponse);
                                model.AttachmentID = fileresult.ID;
                            }
                        }
                    }
                }
            }

            using (var httpClient = new HttpClient())
            {
                StringContent content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
                using (var response = await httpClient.GetAsync("https://localhost:44317/api/Gender/GetAll"))
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        model.Gender = JsonConvert.DeserializeObject <IEnumerable <Gender> >(apiResponse);
                    }
                }

                using (var response = await httpClient.GetAsync("https://localhost:44317/api/Address/GetAll"))
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        model.Address = JsonConvert.DeserializeObject <IEnumerable <City> >(apiResponse);
                    }
                }

                using (var response = await httpClient.PostAsync("https://localhost:44317/Api/Persons/Edit/", content))
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        return(RedirectToAction("Index"));
                    }
                    else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                    {
                        return(View(model));
                    }
                }
            }
            return(View(model));
        }