-
Notifications
You must be signed in to change notification settings - Fork 0
/
Edit.razor
101 lines (89 loc) · 3.17 KB
/
Edit.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
@page "/movies/edit/{id:int}"
@inject BlazorScaffoldingContext DB
@inject NavigationManager NavigationManager
<PageTitle>Edit</PageTitle>
<h1>Edit</h1>
<h4>Movie</h4>
<hr />
@if (Movie is null)
{
<p><em>Loading...</em></p>
}
else
{
<div class="row">
<div class="col-md-4">
<EditForm method="post" Model="Movie" OnValidSubmit="UpdateMovie" FormName="edit">
<DataAnnotationsValidator />
<ValidationSummary />
<input type="hidden" name="Movie.Id" value="@Movie.Id" />
<div class="mb-3">
<label for="title" class="form-label">Title:</label>
<InputText id="title" @bind-Value="Movie.Title" class="form-control" />
<ValidationMessage For="() => Movie.Title" class="text-danger" />
</div>
<div class="mb-3">
<label for="release-date" class="form-label">Release date:</label>
<InputDate id="release-date" @bind-Value="Movie.ReleaseDate" class="form-control" />
<ValidationMessage For="() => Movie.ReleaseDate" class="text-danger" />
</div>
<div class="mb-3">
<label for="genre" class="form-label">Genre:</label>
<InputText id="genre" @bind-Value="Movie.Genre" class="form-control" />
<ValidationMessage For="() => Movie.Genre" class="text-danger" />
</div>
<div class="mb-3">
<label for="price" class="form-label">Price:</label>
<InputNumber id="price" @bind-Value="Movie.Price" min="0" step="0.01" class="form-control" />
<ValidationMessage For="() => Movie.Price" class="text-danger" />
</div>
<button type="submit" class="btn btn-primary">Save</button>
</EditForm>
</div>
</div>
}
<div>
<a href="/movies">Back to List</a>
</div>
@code {
[Parameter]
public int Id { get; set; }
[SupplyParameterFromForm]
public Movie? Movie { get; set; }
protected override async Task OnInitializedAsync()
{
Movie ??= await DB.Movie.FirstOrDefaultAsync(m => m.Id == Id);
if (Movie is null)
{
// Need a way to trigger a 404 here
// https://github.com/dotnet/aspnetcore/issues/45654
}
}
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see https://aka.ms/RazorPagesCRUD.
public async Task UpdateMovie()
{
DB.Attach(Movie!).State = EntityState.Modified;
try
{
await DB.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!MovieExists(Movie!.Id))
{
// Need a way to trigger a 404 here
// https://github.com/dotnet/aspnetcore/issues/45654
}
else
{
throw;
}
}
NavigationManager.NavigateTo("/movies");
}
bool MovieExists(int id)
{
return DB.Movie.Any(e => e.Id == id);
}
}