Skip to content

Commit

Permalink
Add solution for Exercise 14.2
Browse files Browse the repository at this point in the history
  • Loading branch information
markjprice committed Feb 15, 2023
1 parent ea4999b commit 4c70f73
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
17 changes: 17 additions & 0 deletions vs4win/PracticalApps/Northwind.Mvc/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,22 @@ public async Task<IActionResult> Customers(string country)
return View(model);
}

public async Task<IActionResult> CategoryDetail(int? id)
{
if (!id.HasValue)
{
return BadRequest("You must pass a category ID in the route, for example, /Home/CategoryDetail/6");
}

Category? model = await db.Categories.Include(p => p.Products)
.SingleOrDefaultAsync(p => p.CategoryId == id);

if (model is null)
{
return NotFound($"CategoryId {id} not found.");
}

return View(model); // pass model to view and then return result
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@model Packt.Shared.Category
@{
ViewData["Title"] = "Category Detail - " + Model.CategoryName;
}
<h2>Category Detail</h2>
<div>
<dl class="dl-horizontal">
<dt>Category Id</dt>
<dd>@Model.CategoryId</dd>
<dt>Product Name</dt>
<dd>@Model.CategoryName</dd>
<dt>Products</dt>
<dd>@Model.Products.Count</dd>
<dt>Description</dt>
<dd>@Model.Description</dd>
</dl>
</div>
2 changes: 1 addition & 1 deletion vs4win/PracticalApps/Northwind.Mvc/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<h3>@Model.Categories[c].Description</h3>
<p>
<a class="btn btn-primary"
href="/category/@Model.Categories[c].CategoryId">View</a>
href="/home/categorydetail/@Model.Categories[c].CategoryId">View</a>
</p>
</div>
</div>
Expand Down
17 changes: 17 additions & 0 deletions vscode/PracticalApps/Northwind.Mvc/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,22 @@ public async Task<IActionResult> Customers(string country)
return View(model);
}

public async Task<IActionResult> CategoryDetail(int? id)
{
if (!id.HasValue)
{
return BadRequest("You must pass a category ID in the route, for example, /Home/CategoryDetail/6");
}

Category? model = await db.Categories.Include(p => p.Products)
.SingleOrDefaultAsync(p => p.CategoryId == id);

if (model is null)
{
return NotFound($"CategoryId {id} not found.");
}

return View(model); // pass model to view and then return result
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@model Packt.Shared.Category
@{
ViewData["Title"] = "Category Detail - " + Model.CategoryName;
}
<h2>Category Detail</h2>
<div>
<dl class="dl-horizontal">
<dt>Category Id</dt>
<dd>@Model.CategoryId</dd>
<dt>Product Name</dt>
<dd>@Model.CategoryName</dd>
<dt>Products</dt>
<dd>@Model.Products.Count</dd>
<dt>Description</dt>
<dd>@Model.Description</dd>
</dl>
</div>
2 changes: 1 addition & 1 deletion vscode/PracticalApps/Northwind.Mvc/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<h3>@Model.Categories[c].Description</h3>
<p>
<a class="btn btn-primary"
href="/category/@Model.Categories[c].CategoryId">View</a>
href="/home/categorydetail/@Model.Categories[c].CategoryId">View</a>
</p>
</div>
</div>
Expand Down

0 comments on commit 4c70f73

Please sign in to comment.