Skip to content

Commit fa38e1f

Browse files
committed
View and methods for Delete selected Product
Created View for delete Products and methods to request delete an object from the API Rest.
1 parent c932558 commit fa38e1f

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

myClientWebApi/Controllers/ProductsController.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,45 @@ public ActionResult Edit(Product product)
105105

106106
return View(product);
107107
}
108+
109+
public ActionResult Delete(int id)
110+
{
111+
Product product = null;
112+
113+
using (var client = new HttpClient())
114+
{
115+
client.BaseAddress = new Uri(BaseURL);
116+
117+
var responseTask = client.GetAsync("/api/products/" + id.ToString());
118+
responseTask.Wait();
119+
var result = responseTask.Result;
120+
if (result.IsSuccessStatusCode)
121+
{
122+
var readTask = result.Content.ReadAsAsync<Product>();
123+
readTask.Wait();
124+
product = readTask.Result;
125+
}
126+
}
127+
128+
return View(product);
129+
}
130+
131+
[HttpPost]
132+
public ActionResult Delete(Product product, int id)
133+
{
134+
using(var client = new HttpClient())
135+
{
136+
client.BaseAddress = new Uri(BaseURL);
137+
138+
var deleteTask = client.DeleteAsync($"/api/products/{id.ToString()}");
139+
deleteTask.Wait();
140+
var result = deleteTask.Result;
141+
if (result.IsSuccessStatusCode)
142+
{
143+
return RedirectToAction("Index");
144+
}
145+
}
146+
return View(product);
147+
}
108148
}
109149
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
@model myClientWebApi.Models.Product
2+
3+
@{
4+
ViewBag.Title = "Delete Product Selected";
5+
}
6+
7+
<br />
8+
<h2>@ViewBag.Title</h2>
9+
<br />
10+
11+
<h3>Are you sure you want to delete this?</h3>
12+
<div>
13+
<h4>Product</h4>
14+
<hr />
15+
<dl class="dl-horizontal">
16+
<dt>
17+
@Html.DisplayNameFor(model => model.ProductName)
18+
</dt>
19+
20+
<dd>
21+
@Html.DisplayFor(model => model.ProductName)
22+
</dd>
23+
24+
<dt>
25+
@Html.DisplayNameFor(model => model.SupplierID)
26+
</dt>
27+
28+
<dd>
29+
@Html.DisplayFor(model => model.SupplierID)
30+
</dd>
31+
32+
<dt>
33+
@Html.DisplayNameFor(model => model.CategoryID)
34+
</dt>
35+
36+
<dd>
37+
@Html.DisplayFor(model => model.CategoryID)
38+
</dd>
39+
40+
<dt>
41+
@Html.DisplayNameFor(model => model.QuantityPerUnit)
42+
</dt>
43+
44+
<dd>
45+
@Html.DisplayFor(model => model.QuantityPerUnit)
46+
</dd>
47+
48+
<dt>
49+
@Html.DisplayNameFor(model => model.UnitPrice)
50+
</dt>
51+
52+
<dd>
53+
@Html.DisplayFor(model => model.UnitPrice)
54+
</dd>
55+
56+
<dt>
57+
@Html.DisplayNameFor(model => model.UnitsInStock)
58+
</dt>
59+
60+
<dd>
61+
@Html.DisplayFor(model => model.UnitsInStock)
62+
</dd>
63+
64+
<dt>
65+
@Html.DisplayNameFor(model => model.UnitsOnOrder)
66+
</dt>
67+
68+
<dd>
69+
@Html.DisplayFor(model => model.UnitsOnOrder)
70+
</dd>
71+
72+
<dt>
73+
@Html.DisplayNameFor(model => model.ReorderLevel)
74+
</dt>
75+
76+
<dd>
77+
@Html.DisplayFor(model => model.ReorderLevel)
78+
</dd>
79+
80+
<dt>
81+
@Html.DisplayNameFor(model => model.Discontinued)
82+
</dt>
83+
84+
<dd>
85+
@Html.DisplayFor(model => model.Discontinued)
86+
</dd>
87+
88+
</dl>
89+
90+
@using (Html.BeginForm())
91+
{
92+
@Html.AntiForgeryToken()
93+
94+
<div class="form-actions no-color">
95+
<input type="submit" value="Delete" class="btn btn-danger" />
96+
@Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-default" })
97+
</div>
98+
}
99+
</div>

myClientWebApi/myClientWebApi.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
<Content Include="Views\Products\Index.cshtml" />
177177
<Content Include="Views\Products\Create.cshtml" />
178178
<Content Include="Views\Products\Edit.cshtml" />
179+
<Content Include="Views\Products\Delete.cshtml" />
179180
<None Include="Web.Debug.config">
180181
<DependentUpon>Web.config</DependentUpon>
181182
</None>

0 commit comments

Comments
 (0)