lundi 23 septembre 2019

Post request not updating Database

I have a MVC web app with an API project. The Post request does not seem to work. I am following this tutorial.

https://www.tutorialsteacher.com/webapi/consume-web-api-put-method-in-aspnet-mvc

This is what I have:

public ActionResult Edit(int id)
    {
        Employees employee = null;

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("https://localhost:44339/api/employees");
            //HTTP GET
            var responseTask = client.GetAsync("employees/" + id.ToString());
            responseTask.Wait();



            var result = responseTask.Result;
            if (result.IsSuccessStatusCode)
            {
                var readTask = result.Content.ReadAsAsync<Employees>();
                readTask.Wait();

                employee = readTask.Result;
            }
        }

        return View(employee);
    }

    [HttpPost]
    public ActionResult Edit(Employees employee)
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("https://localhost:44339/api/employees");

            //HTTP POST
            var putTask = client.PutAsJsonAsync<Employees>("employees", employee);
            putTask.Wait();

            var result = putTask.Result;
            if (result.IsSuccessStatusCode)
            {

                return RedirectToAction("ApiTest");
            }
        }
        return View(employee);
    }

It just returns me back to ApiTest with no update happening.




Aucun commentaire:

Enregistrer un commentaire