Пример #1
0
        public async Task <IActionResult> Edit(int id, [Bind("OrderId,CustomerId,OrderStatus,OrderDate,RequiredDate,ShippedDate,StoreId,StaffId")] data.Orders orders)
        {
            if (id != orders.OrderId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    using (var cl = new HttpClient())
                    {
                        cl.BaseAddress = new Uri(baseurl);
                        var content     = JsonConvert.SerializeObject(orders);
                        var buffer      = System.Text.Encoding.UTF8.GetBytes(content);
                        var byteContent = new ByteArrayContent(buffer);
                        byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                        var postTask = cl.PutAsync("api/Orders/" + id, byteContent).Result;

                        if (postTask.IsSuccessStatusCode)
                        {
                            return(RedirectToAction("Index"));
                        }
                    }
                }
                catch (Exception)
                {
                    var aux2 = GetById(id);
                    if (aux2 == null)
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CustomerId"] = new SelectList(Listas.getAllCustomers(), "CustomerId", "Email", orders.CustomerId);
            ViewData["StaffId"]    = new SelectList(Listas.getAllStaffs(), "StaffId", "Email", orders.StaffId);
            ViewData["StoreId"]    = new SelectList(Listas.getAllStores(), "StoreId", "StoreName", orders.StoreId);
            return(View(orders));
        }
Пример #2
0
        private data.Orders GetById(int?id)
        {
            data.Orders aux = new data.Orders();
            using (var cl = new HttpClient())
            {
                cl.BaseAddress = new Uri(baseurl);
                cl.DefaultRequestHeaders.Clear();
                cl.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage res = cl.GetAsync("api/Orders/" + id).Result;

                if (res.IsSuccessStatusCode)
                {
                    var auxres = res.Content.ReadAsStringAsync().Result;
                    aux = JsonConvert.DeserializeObject <data.Orders>(auxres);
                }
            }
            return(aux);
        }