diff --git a/vs4win/PracticalApps/Northwind.Mvc/Controllers/HomeController.cs b/vs4win/PracticalApps/Northwind.Mvc/Controllers/HomeController.cs index 068b4a9..801a961 100644 --- a/vs4win/PracticalApps/Northwind.Mvc/Controllers/HomeController.cs +++ b/vs4win/PracticalApps/Northwind.Mvc/Controllers/HomeController.cs @@ -164,5 +164,22 @@ namespace Northwind.Mvc.Controllers return View(model); } + public async Task 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 + } } } \ No newline at end of file diff --git a/vs4win/PracticalApps/Northwind.Mvc/Views/Home/CategoryDetail.cshtml b/vs4win/PracticalApps/Northwind.Mvc/Views/Home/CategoryDetail.cshtml new file mode 100644 index 0000000..9c39c5d --- /dev/null +++ b/vs4win/PracticalApps/Northwind.Mvc/Views/Home/CategoryDetail.cshtml @@ -0,0 +1,17 @@ +@model Packt.Shared.Category +@{ + ViewData["Title"] = "Category Detail - " + Model.CategoryName; +} +

Category Detail

+
+
+
Category Id
+
@Model.CategoryId
+
Product Name
+
@Model.CategoryName
+
Products
+
@Model.Products.Count
+
Description
+
@Model.Description
+
+
diff --git a/vs4win/PracticalApps/Northwind.Mvc/Views/Home/Index.cshtml b/vs4win/PracticalApps/Northwind.Mvc/Views/Home/Index.cshtml index 7cfb78c..45ded79 100644 --- a/vs4win/PracticalApps/Northwind.Mvc/Views/Home/Index.cshtml +++ b/vs4win/PracticalApps/Northwind.Mvc/Views/Home/Index.cshtml @@ -69,7 +69,7 @@

@Model.Categories[c].Description

View + href="/home/categorydetail/@Model.Categories[c].CategoryId">View

diff --git a/vscode/PracticalApps/Northwind.Mvc/Controllers/HomeController.cs b/vscode/PracticalApps/Northwind.Mvc/Controllers/HomeController.cs index 068b4a9..801a961 100644 --- a/vscode/PracticalApps/Northwind.Mvc/Controllers/HomeController.cs +++ b/vscode/PracticalApps/Northwind.Mvc/Controllers/HomeController.cs @@ -164,5 +164,22 @@ namespace Northwind.Mvc.Controllers return View(model); } + public async Task 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 + } } } \ No newline at end of file diff --git a/vscode/PracticalApps/Northwind.Mvc/Views/Home/CategoryDetail.cshtml b/vscode/PracticalApps/Northwind.Mvc/Views/Home/CategoryDetail.cshtml new file mode 100644 index 0000000..9c39c5d --- /dev/null +++ b/vscode/PracticalApps/Northwind.Mvc/Views/Home/CategoryDetail.cshtml @@ -0,0 +1,17 @@ +@model Packt.Shared.Category +@{ + ViewData["Title"] = "Category Detail - " + Model.CategoryName; +} +

Category Detail

+
+
+
Category Id
+
@Model.CategoryId
+
Product Name
+
@Model.CategoryName
+
Products
+
@Model.Products.Count
+
Description
+
@Model.Description
+
+
diff --git a/vscode/PracticalApps/Northwind.Mvc/Views/Home/Index.cshtml b/vscode/PracticalApps/Northwind.Mvc/Views/Home/Index.cshtml index 7cfb78c..45ded79 100644 --- a/vscode/PracticalApps/Northwind.Mvc/Views/Home/Index.cshtml +++ b/vscode/PracticalApps/Northwind.Mvc/Views/Home/Index.cshtml @@ -69,7 +69,7 @@

@Model.Categories[c].Description

View + href="/home/categorydetail/@Model.Categories[c].CategoryId">View