mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-03-30 01:04:38 +02:00
Add solution for Exercise 14.2
This commit is contained in:
parent
ea4999b34a
commit
4c70f73328
|
|
@ -164,5 +164,22 @@ namespace Northwind.Mvc.Controllers
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -164,5 +164,22 @@ namespace Northwind.Mvc.Controllers
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Reference in a new issue