Skip to content

Commit cf5c884

Browse files
5.0 update for EF/RP (#20064)
* 5.0 update for EF/RP * 5.0 update for EF/RP * 5.0 update for EF/RP * 5.0 update for EF/RP
1 parent 9205b02 commit cf5c884

10 files changed

Lines changed: 267 additions & 58 deletions

File tree

aspnetcore/data/ef-rp/intro/samples/cu30/Pages/Students/Index.cshtml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#region snippet_All
2-
using ContosoUniversity.Data;
1+
using ContosoUniversity.Data;
32
using ContosoUniversity.Models;
43
using Microsoft.AspNetCore.Mvc.RazorPages;
54
using Microsoft.EntityFrameworkCore;
@@ -10,6 +9,7 @@
109

1110
namespace ContosoUniversity.Pages.Students
1211
{
12+
#region snippet_All
1313
public class IndexModel : PageModel
1414
{
1515
private readonly SchoolContext _context;
@@ -71,5 +71,5 @@ public async Task OnGetAsync(string sortOrder,
7171
studentsIQ.AsNoTracking(), pageIndex ?? 1, pageSize);
7272
}
7373
}
74+
#endregion
7475
}
75-
#endregion

aspnetcore/data/ef-rp/intro/samples/cu30snapshots/3-sorting/Pages/Students/Index1.cshtml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#region snippet_All
21
using ContosoUniversity.Data;
32
using ContosoUniversity.Models;
43
using Microsoft.AspNetCore.Mvc.RazorPages;
@@ -10,10 +9,10 @@
109

1110
namespace ContosoUniversity.Pages.Students
1211
{
12+
#region snippet_All
1313
public class IndexModel : PageModel
1414
{
1515
private readonly SchoolContext _context;
16-
1716
public IndexModel(SchoolContext context)
1817
{
1918
_context = context;
@@ -28,6 +27,7 @@ public IndexModel(SchoolContext context)
2827

2928
public async Task OnGetAsync(string sortOrder)
3029
{
30+
// using System;
3131
#region snippet_Ternary
3232
NameSort = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
3333
DateSort = sortOrder == "Date" ? "date_desc" : "Date";
@@ -59,5 +59,5 @@ public async Task OnGetAsync(string sortOrder)
5959
#endregion
6060
}
6161
}
62+
#endregion
6263
}
63-
#endregion

aspnetcore/data/ef-rp/intro/samples/cu30snapshots/3-sorting/Pages/Students/Index2.cshtml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#region snippet_All
2-
using ContosoUniversity.Data;
1+
using ContosoUniversity.Data;
32
using ContosoUniversity.Models;
43
using Microsoft.AspNetCore.Mvc.RazorPages;
54
using Microsoft.EntityFrameworkCore;
@@ -10,6 +9,7 @@
109

1110
namespace ContosoUniversity.Pages.Students
1211
{
12+
#region snippet_All
1313
public class IndexModel : PageModel
1414
{
1515
private readonly SchoolContext _context;
@@ -60,5 +60,5 @@ public async Task OnGetAsync(string sortOrder, string searchString)
6060
Students = await studentsIQ.AsNoTracking().ToListAsync();
6161
}
6262
}
63+
#endregion
6364
}
64-
#endregion
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace ContosoUniversity.Models.SchoolViewModels
5+
{
6+
public class EnrollmentDateGroup
7+
{
8+
[DataType(DataType.Date)]
9+
public DateTime? EnrollmentDate { get; set; }
10+
11+
public int StudentCount { get; set; }
12+
}
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
@page
2+
@model ContosoUniversity.Pages.AboutModel
3+
4+
@{
5+
ViewData["Title"] = "Student Body Statistics";
6+
}
7+
8+
<h2>Student Body Statistics</h2>
9+
10+
<table>
11+
<tr>
12+
<th>
13+
Enrollment Date
14+
</th>
15+
<th>
16+
Students
17+
</th>
18+
</tr>
19+
20+
@foreach (var item in Model.Students)
21+
{
22+
<tr>
23+
<td>
24+
@Html.DisplayFor(modelItem => item.EnrollmentDate)
25+
</td>
26+
<td>
27+
@item.StudentCount
28+
</td>
29+
</tr>
30+
}
31+
</table>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using ContosoUniversity.Models.SchoolViewModels;
2+
using ContosoUniversity.Data;
3+
using Microsoft.AspNetCore.Mvc.RazorPages;
4+
using Microsoft.EntityFrameworkCore;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using ContosoUniversity.Models;
9+
10+
namespace ContosoUniversity.Pages
11+
{
12+
public class AboutModel : PageModel
13+
{
14+
private readonly SchoolContext _context;
15+
16+
public AboutModel(SchoolContext context)
17+
{
18+
_context = context;
19+
}
20+
21+
public IList<EnrollmentDateGroup> Students { get; set; }
22+
23+
public async Task OnGetAsync()
24+
{
25+
IQueryable<EnrollmentDateGroup> data =
26+
from student in _context.Students
27+
group student by student.EnrollmentDate into dateGroup
28+
select new EnrollmentDateGroup()
29+
{
30+
EnrollmentDate = dateGroup.Key,
31+
StudentCount = dateGroup.Count()
32+
};
33+
34+
Students = await data.AsNoTracking().ToListAsync();
35+
}
36+
}
37+
}

aspnetcore/data/ef-rp/intro/samples/cu50/Pages/Students/Index.cshtml

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,86 @@
22
@model ContosoUniversity.Pages.Students.IndexModel
33

44
@{
5-
ViewData["Title"] = "Index";
5+
ViewData["Title"] = "Students";
66
}
77

8-
<h1>Index</h1>
8+
<h2>Students</h2>
99

1010
<p>
1111
<a asp-page="Create">Create New</a>
1212
</p>
13+
14+
<form asp-page="./Index" method="get">
15+
<div class="form-actions no-color">
16+
<p>
17+
Find by name:
18+
<input type="text" name="SearchString" value="@Model.CurrentFilter" />
19+
<input type="submit" value="Search" class="btn btn-primary" /> |
20+
<a asp-page="./Index">Back to full List</a>
21+
</p>
22+
</div>
23+
</form>
24+
1325
<table class="table">
1426
<thead>
1527
<tr>
1628
<th>
17-
@Html.DisplayNameFor(model => model.Student[0].LastName)
29+
<a asp-page="./Index" asp-route-sortOrder="@Model.NameSort"
30+
asp-route-currentFilter="@Model.CurrentFilter">
31+
@Html.DisplayNameFor(model => model.Students[0].LastName)
32+
</a>
1833
</th>
1934
<th>
20-
@Html.DisplayNameFor(model => model.Student[0].FirstMidName)
35+
@Html.DisplayNameFor(model => model.Students[0].FirstMidName)
2136
</th>
2237
<th>
23-
@Html.DisplayNameFor(model => model.Student[0].EnrollmentDate)
38+
<a asp-page="./Index" asp-route-sortOrder="@Model.DateSort"
39+
asp-route-currentFilter="@Model.CurrentFilter">
40+
@Html.DisplayNameFor(model => model.Students[0].EnrollmentDate)
41+
</a>
2442
</th>
2543
<th></th>
2644
</tr>
2745
</thead>
2846
<tbody>
29-
@foreach (var item in Model.Student) {
30-
<tr>
31-
<td>
32-
@Html.DisplayFor(modelItem => item.LastName)
33-
</td>
34-
<td>
35-
@Html.DisplayFor(modelItem => item.FirstMidName)
36-
</td>
37-
<td>
38-
@Html.DisplayFor(modelItem => item.EnrollmentDate)
39-
</td>
40-
<td>
41-
<a asp-page="./Edit" asp-route-id="@item.ID">Edit</a> |
42-
<a asp-page="./Details" asp-route-id="@item.ID">Details</a> |
43-
<a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>
44-
</td>
45-
</tr>
46-
}
47+
@foreach (var item in Model.Students)
48+
{
49+
<tr>
50+
<td>
51+
@Html.DisplayFor(modelItem => item.LastName)
52+
</td>
53+
<td>
54+
@Html.DisplayFor(modelItem => item.FirstMidName)
55+
</td>
56+
<td>
57+
@Html.DisplayFor(modelItem => item.EnrollmentDate)
58+
</td>
59+
<td>
60+
<a asp-page="./Edit" asp-route-id="@item.ID">Edit</a> |
61+
<a asp-page="./Details" asp-route-id="@item.ID">Details</a> |
62+
<a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>
63+
</td>
64+
</tr>
65+
}
4766
</tbody>
4867
</table>
68+
69+
@{
70+
var prevDisabled = !Model.Students.HasPreviousPage ? "disabled" : "";
71+
var nextDisabled = !Model.Students.HasNextPage ? "disabled" : "";
72+
}
73+
74+
<a asp-page="./Index"
75+
asp-route-sortOrder="@Model.CurrentSort"
76+
asp-route-pageIndex="@(Model.Students.PageIndex - 1)"
77+
asp-route-currentFilter="@Model.CurrentFilter"
78+
class="btn btn-primary @prevDisabled">
79+
Previous
80+
</a>
81+
<a asp-page="./Index"
82+
asp-route-sortOrder="@Model.CurrentSort"
83+
asp-route-pageIndex="@(Model.Students.PageIndex + 1)"
84+
asp-route-currentFilter="@Model.CurrentFilter"
85+
class="btn btn-primary @nextDisabled">
86+
Next
87+
</a>
Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,73 @@
11
using ContosoUniversity.Data;
22
using ContosoUniversity.Models;
3-
using Microsoft.AspNetCore.Mvc;
43
using Microsoft.AspNetCore.Mvc.RazorPages;
54
using Microsoft.EntityFrameworkCore;
6-
using Microsoft.Extensions.Options;
5+
using System;
76
using System.Collections.Generic;
87
using System.Linq;
98
using System.Threading.Tasks;
109

1110
namespace ContosoUniversity.Pages.Students
1211
{
13-
#region snippet
1412
public class IndexModel : PageModel
1513
{
1614
private readonly SchoolContext _context;
17-
private readonly MvcOptions _mvcOptions;
1815

19-
public IndexModel(SchoolContext context, IOptions<MvcOptions> mvcOptions)
16+
public IndexModel(SchoolContext context)
2017
{
2118
_context = context;
22-
_mvcOptions = mvcOptions.Value;
2319
}
2420

25-
public IList<Student> Student { get;set; }
21+
public string NameSort { get; set; }
22+
public string DateSort { get; set; }
23+
public string CurrentFilter { get; set; }
24+
public string CurrentSort { get; set; }
2625

27-
public async Task OnGetAsync()
26+
public PaginatedList<Student> Students { get; set; }
27+
28+
public async Task OnGetAsync(string sortOrder,
29+
string currentFilter, string searchString, int? pageIndex)
2830
{
29-
Student = await _context.Students.Take(
30-
_mvcOptions.MaxModelBindingCollectionSize).ToListAsync();
31+
CurrentSort = sortOrder;
32+
NameSort = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
33+
DateSort = sortOrder == "Date" ? "date_desc" : "Date";
34+
if (searchString != null)
35+
{
36+
pageIndex = 1;
37+
}
38+
else
39+
{
40+
searchString = currentFilter;
41+
}
42+
43+
CurrentFilter = searchString;
44+
45+
IQueryable<Student> studentsIQ = from s in _context.Students
46+
select s;
47+
if (!String.IsNullOrEmpty(searchString))
48+
{
49+
studentsIQ = studentsIQ.Where(s => s.LastName.Contains(searchString)
50+
|| s.FirstMidName.Contains(searchString));
51+
}
52+
switch (sortOrder)
53+
{
54+
case "name_desc":
55+
studentsIQ = studentsIQ.OrderByDescending(s => s.LastName);
56+
break;
57+
case "Date":
58+
studentsIQ = studentsIQ.OrderBy(s => s.EnrollmentDate);
59+
break;
60+
case "date_desc":
61+
studentsIQ = studentsIQ.OrderByDescending(s => s.EnrollmentDate);
62+
break;
63+
default:
64+
studentsIQ = studentsIQ.OrderBy(s => s.LastName);
65+
break;
66+
}
67+
68+
int pageSize = 3;
69+
Students = await PaginatedList<Student>.CreateAsync(
70+
studentsIQ.AsNoTracking(), pageIndex ?? 1, pageSize);
3171
}
3272
}
33-
#endregion
3473
}

0 commit comments

Comments
 (0)