2
Reply

Web API and MVC Project Using [HttpPost] To Delete

Luke Rayner

Luke Rayner

Nov 20 2015 3:39 AM
385
Hi,
I have been making a project which involves a Web API and MVC, I have been able to connect my MVC project to my API allowing me to display data onto one of my views using a table.
I am struggling to create a delete function on my MVC project, I have made it pull a set of data from the table to a different view using the ID (i have created the view using the template you are given)
The code below is what i did in order to get that data
[HttpGet]
public ActionResult Delete(int id)
{
StudentModel Student = new StudentModel();
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:6193/");

client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/students/" + id).Result;
if (response.IsSuccessStatusCode)
{
Student = response.Content.ReadAsAsync<StudentModel>().Result;

}
return View(Student);
}
Now i am trying to use the [HttpPost] to remove the data from my database i would be really grateful if anyone is able to help me out and keep it pretty simple so i am able to understand it.

Upload Source Code:  Select only zip and rar file.

Answers (2)