Skip to content

Commit e0eab15

Browse files
committed
Details
1 parent ab14bc2 commit e0eab15

File tree

4 files changed

+70
-41
lines changed

4 files changed

+70
-41
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using AutoMapper.QueryableExtensions;
7+
using ContosoUniversity.Data;
8+
using ContosoUniversity.Models;
9+
using MediatR;
10+
using Microsoft.EntityFrameworkCore;
11+
12+
namespace ContosoUniversity.Features.Students
13+
{
14+
public class Details
15+
{
16+
public class Query : IRequest<Model>
17+
{
18+
public int Id { get; set; }
19+
}
20+
21+
public class Model
22+
{
23+
public int ID { get; set; }
24+
[Display(Name = "First Name")]
25+
public string FirstMidName { get; set; }
26+
public string LastName { get; set; }
27+
public DateTime EnrollmentDate { get; set; }
28+
public List<Enrollment> Enrollments { get; set; }
29+
30+
public class Enrollment
31+
{
32+
public string CourseTitle { get; set; }
33+
public Grade? Grade { get; set; }
34+
}
35+
}
36+
37+
public class Handler : IAsyncRequestHandler<Query, Model>
38+
{
39+
private readonly SchoolContext _db;
40+
41+
public Handler(SchoolContext db) => _db = db;
42+
43+
public async Task<Model> Handle(Query message) => await _db
44+
.Students
45+
.Include(m => m.Enrollments)
46+
.ThenInclude(e => e.Course)
47+
.Where(s => s.Id == message.Id)
48+
.ProjectTo<Model>()
49+
.SingleOrDefaultAsync();
50+
}
51+
}
52+
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@model ContosoUniversity.Models.Student
1+
@model ContosoUniversity.Features.Students.Details.Model
22

33
@{
44
ViewData["Title"] = "Details";
@@ -11,40 +11,40 @@
1111
<hr />
1212
<dl class="dl-horizontal">
1313
<dt>
14-
@Html.DisplayNameFor(model => model.LastName)
14+
<display-label-tag for="LastName"/>
1515
</dt>
1616
<dd>
17-
@Html.DisplayFor(model => model.LastName)
17+
<display-tag for="LastName"/>
1818
</dd>
1919
<dt>
20-
@Html.DisplayNameFor(model => model.FirstMidName)
20+
<display-label-tag for="FirstMidName" />
2121
</dt>
2222
<dd>
23-
@Html.DisplayFor(model => model.FirstMidName)
23+
<display-tag for="FirstMidName" />
2424
</dd>
2525
<dt>
26-
@Html.DisplayNameFor(model => model.EnrollmentDate)
26+
<display-label-tag for="EnrollmentDate" />
2727
</dt>
2828
<dd>
29-
@Html.DisplayFor(model => model.EnrollmentDate)
29+
<display-tag for="EnrollmentDate" />
3030
</dd>
3131
<dt>
32-
@Html.DisplayNameFor(model => model.Enrollments)
32+
<display-label-tag for="Enrollments" />
3333
</dt>
3434
<dd>
3535
<table class="table">
3636
<tr>
3737
<th>Course Title</th>
3838
<th>Grade</th>
3939
</tr>
40-
@foreach (var item in Model.Enrollments)
40+
@for (var i = 0; i < Model.Enrollments.Count; i++)
4141
{
4242
<tr>
4343
<td>
44-
@Html.DisplayFor(modelItem => item.Course.Title)
44+
<display-tag for="Enrollments[i].CourseTitle" />
4545
</td>
4646
<td>
47-
@Html.DisplayFor(modelItem => item.Grade)
47+
<display-tag for="Enrollments[i].Grade" />
4848
</td>
4949
</tr>
5050
}
@@ -53,6 +53,6 @@
5353
</dl>
5454
</div>
5555
<div>
56-
<a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
56+
<a asp-action="Edit" asp-route-id="@Model.ID">Edit</a> |
5757
<a asp-action="Index">Back to List</a>
5858
</div>

ContosoUniversity/Features/Students/MappingProfile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public class MappingProfile : Profile
88
public MappingProfile()
99
{
1010
CreateMap<Student, Index.Model>();
11-
//CreateMap<Student, Details.Model>();
12-
//CreateMap<Enrollment, Details.Model.Enrollment>();
11+
CreateMap<Student, Details.Model>();
12+
CreateMap<Enrollment, Details.Model.Enrollment>();
1313
//CreateMap<Create.Command, Student>(MemberList.Source);
1414
//CreateMap<Student, Edit.Command>().ReverseMap();
1515
//CreateMap<Student, Delete.Command>().ReverseMap();

ContosoUniversity/Features/Students/StudentsController.cs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,11 @@ public StudentsController(SchoolContext context, IMediator mediator)
2020
_mediator = mediator;
2121
}
2222

23-
public async Task<ViewResult> Index(Index.Query query)
24-
{
25-
var model = await _mediator.Send(query);
26-
27-
return View(model);
28-
}
29-
30-
// GET: Students/Details/5
31-
public async Task<IActionResult> Details(int? id)
32-
{
33-
if (id == null)
34-
{
35-
return NotFound();
36-
}
37-
38-
var student = await _context.Students
39-
.Include(s => s.Enrollments)
40-
.ThenInclude(e => e.Course)
41-
.AsNoTracking()
42-
.SingleOrDefaultAsync(m => m.Id == id);
43-
44-
if (student == null)
45-
{
46-
return NotFound();
47-
}
23+
public async Task<ViewResult> Index(Index.Query query)
24+
=> View(await _mediator.Send(query));
4825

49-
return View(student);
50-
}
26+
public async Task<IActionResult> Details(Details.Query query)
27+
=> View(await _mediator.Send(query));
5128

5229
// GET: Students/Create
5330
public IActionResult Create()

0 commit comments

Comments
 (0)