Skip to content

Commit 9243cab

Browse files
committedJul 22, 2023
Add code solutions for Ch13
1 parent db12c4f commit 9243cab

26 files changed

+874
-5
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@page
2+
@using Northwind.EntityModels
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4+
@model PacktFeatures.Pages.EmployeesListPageModel
5+
6+
<div class="row">
7+
<h1 class="display-2">Employees</h1>
8+
</div>
9+
<div class="row">
10+
@foreach (Employee employee in Model.Employees)
11+
{
12+
<div class="col-sm-3">
13+
<!--This will look in Pages\Shared for _Employee.cshtml
14+
and pass the employee model to it.-->
15+
<partial name="_Employee" model="employee" />
16+
</div>
17+
}
18+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.AspNetCore.Mvc.RazorPages; // To use PageModel.
2+
using Northwind.EntityModels; // To use Employee, NorthwindContext.
3+
4+
namespace PacktFeatures.Pages;
5+
6+
public class EmployeesListPageModel : PageModel
7+
{
8+
private NorthwindContext _db;
9+
10+
public EmployeesListPageModel(NorthwindContext db)
11+
{
12+
_db = db;
13+
}
14+
15+
public Employee[] Employees { get; set; } = null!;
16+
17+
public void OnGet()
18+
{
19+
ViewData["Title"] = "Northwind B2B - Employees";
20+
21+
Employees = _db.Employees.OrderBy(e => e.LastName)
22+
.ThenBy(e => e.FirstName).ToArray();
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@model Northwind.EntityModels.Employee
2+
<div class="card border-dark mb-3" style="max-width: 18rem;">
3+
<div class="card-header">@Model?.LastName, @Model?.FirstName</div>
4+
<div class="card-body text-dark">
5+
<h5 class="card-title">@Model?.Country</h5>
6+
<p class="card-text">@Model?.Notes</p>
7+
</div>
8+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
Layout = "_Layout";
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
12+
</ItemGroup>
13+
14+
<!-- change Sqlite to SqlServer if you prefer -->
15+
<ItemGroup>
16+
<ProjectReference Include="..\Northwind.DataContext.Sqlite\Northwind.DataContext.Sqlite.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Using Include="System.Console" Static="true" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<!-- change Sqlite to SqlServer if you prefer -->
15+
<ProjectReference Include="..\Northwind.DataContext.Sqlite\Northwind.DataContext.Sqlite.csproj" />
16+
<ProjectReference Include="..\Northwind.Razor.Employees\Northwind.Razor.Employees.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ActiveDebugProfile>https</ActiveDebugProfile>
5+
</PropertyGroup>
6+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@page
2+
@using Northwind.Web.Pages
3+
@using Northwind.EntityModels
4+
@model CustomerOrdersModel
5+
@{
6+
string title = "Customer and their orders";
7+
ViewData["Title"] = $"Northwind B2B - {title}";
8+
}
9+
<div class="row">
10+
<h1 class="display-2">@title</h1>
11+
<div>
12+
@if (Model.Customer is not null)
13+
{
14+
<div>
15+
<div>@Model.Customer.CompanyName</div>
16+
</div>
17+
<div>
18+
<table>
19+
<thead>
20+
<tr><th>Order Id</th><th>Order Date</th></tr>
21+
</thead>
22+
<tbody>
23+
@foreach (Order o in Model.Customer.Orders)
24+
{
25+
<tr><td>@o.OrderId</td><td>@o.OrderDate</td></tr>
26+
}
27+
</tbody>
28+
</table>
29+
</div>
30+
}
31+
</div>
32+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.AspNetCore.Mvc.RazorPages; // To use PageModel.
2+
using Microsoft.EntityFrameworkCore; // To use Include method.
3+
using Northwind.EntityModels; // To use Customer.
4+
5+
namespace Northwind.Web.Pages;
6+
7+
public class CustomerOrdersModel : PageModel
8+
{
9+
public Customer? Customer;
10+
11+
private NorthwindContext _db;
12+
13+
public CustomerOrdersModel(NorthwindContext db)
14+
{
15+
_db = db;
16+
}
17+
18+
public void OnGet()
19+
{
20+
string? id = HttpContext.Request.Query["id"];
21+
22+
Customer = _db.Customers.Include(c => c.Orders)
23+
.SingleOrDefault(c => c.CustomerId == id);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@page
2+
@using Northwind.Web.Pages
3+
@using Northwind.EntityModels
4+
@model CustomersModel
5+
@{
6+
string title = "Customers by Country";
7+
ViewData["Title"] = $"Northwind B2B - {title}";
8+
}
9+
<div class="row">
10+
<h1 class="display-2">@title</h1>
11+
<div>
12+
<h2>Exercise 13.4 – Practice building a data-driven web page</h2>
13+
</div>
14+
<div class="accordion" id="accordionCustomers">
15+
@if (Model.CustomersByCountry is not null)
16+
{
17+
@foreach (IGrouping<string?, Customer> cbc in Model.CustomersByCountry)
18+
{
19+
<div class="accordion-item">
20+
<h2 class="accordion-header" id="header@(cbc.Key)">
21+
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse@(cbc.Key)" aria-expanded="true" aria-controls="collapse@(cbc.Key)">
22+
@cbc.Key has @cbc.Count() customers
23+
</button>
24+
</h2>
25+
<div id="collapse@(cbc.Key)" class="accordion-collapse collapse" aria-labelledby="heading@(cbc.Key)" data-bs-parent="#accordionCustomers">
26+
<div class="accordion-body">
27+
<ul>
28+
@foreach (Customer c in cbc)
29+
{
30+
<li><a href="customerorders?id=@c.CustomerId">
31+
@c.CompanyName</a></li>
32+
}
33+
</ul>
34+
</div>
35+
</div>
36+
</div>
37+
}
38+
}
39+
</div>
40+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.AspNetCore.Mvc.RazorPages; // To use PageModel.
2+
using Northwind.EntityModels; // To use Customer.
3+
4+
namespace Northwind.Web.Pages;
5+
6+
public class CustomersModel : PageModel
7+
{
8+
public ILookup<string?, Customer>? CustomersByCountry;
9+
10+
private NorthwindContext _db;
11+
12+
public CustomersModel(NorthwindContext db)
13+
{
14+
_db = db;
15+
}
16+
17+
public void OnGet()
18+
{
19+
CustomersByCountry = _db.Customers.ToLookup(c => c.Country);
20+
}
21+
}

0 commit comments

Comments
 (0)
Please sign in to comment.