File tree Expand file tree Collapse file tree 2 files changed +94
-0
lines changed
AspNetRunBasic/Pages/Product Expand file tree Collapse file tree 2 files changed +94
-0
lines changed Original file line number Diff line number Diff line change 1+ @page
2+ @model AspNetRunBasic .Pages .Product .DeleteModel
3+ @{
4+ ViewData [" Title" ] = " Delete" ;
5+ }
6+
7+ <h1 >Delete</h1 >
8+
9+ <h3 >Are you sure you want to delete this?</h3 >
10+ <div >
11+ <h4 >Product</h4 >
12+ <hr />
13+ <dl class =" row" >
14+ <dt class =" col-sm-2" >
15+ @Html.DisplayNameFor(model => model.Product.Name)
16+ </dt >
17+ <dd class =" col-sm-10" >
18+ @Html.DisplayFor(model => model.Product.Name)
19+ </dd >
20+ <dt class =" col-sm-2" >
21+ @Html.DisplayNameFor(model => model.Product.Description)
22+ </dt >
23+ <dd class =" col-sm-10" >
24+ @Html.DisplayFor(model => model.Product.Description)
25+ </dd >
26+ <dt class =" col-sm-2" >
27+ @Html.DisplayNameFor(model => model.Product.UnitPrice)
28+ </dt >
29+ <dd class =" col-sm-10" >
30+ @Html.DisplayFor(model => model.Product.UnitPrice)
31+ </dd >
32+ <dt class =" col-sm-2" >
33+ @Html.DisplayNameFor(model => model.Product.Category)
34+ </dt >
35+ <dd class =" col-sm-10" >
36+ @Html.DisplayFor(model => model.Product.Category.Name)
37+ </dd >
38+ </dl >
39+
40+ <form method =" post" >
41+ <input type =" hidden" asp-for =" Product.Id" />
42+ <input type =" submit" value =" Delete" class =" btn btn-danger" /> |
43+ <a asp-page =" ./Index" >Back to List</a >
44+ </form >
45+ </div >
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Threading . Tasks ;
5+ using AspNetRunBasic . Repositories ;
6+ using Microsoft . AspNetCore . Mvc ;
7+ using Microsoft . AspNetCore . Mvc . RazorPages ;
8+
9+ namespace AspNetRunBasic . Pages . Product
10+ {
11+ public class DeleteModel : PageModel
12+ {
13+ private readonly IProductRepository _productRepository ;
14+
15+ public DeleteModel ( IProductRepository productRepository )
16+ {
17+ _productRepository = productRepository ?? throw new ArgumentNullException ( nameof ( productRepository ) ) ;
18+ }
19+
20+ [ BindProperty ]
21+ public Entities . Product Product { get ; set ; }
22+
23+ public async Task < IActionResult > OnGetAsync ( int ? productId )
24+ {
25+ if ( productId == null )
26+ {
27+ return NotFound ( ) ;
28+ }
29+
30+ Product = await _productRepository . GetProductByIdAsync ( productId . Value ) ;
31+ if ( Product == null )
32+ {
33+ return NotFound ( ) ;
34+ }
35+ return Page ( ) ;
36+ }
37+
38+ public async Task < IActionResult > OnPostAsync ( int ? productId )
39+ {
40+ if ( productId == null )
41+ {
42+ return NotFound ( ) ;
43+ }
44+
45+ await _productRepository . DeleteAsync ( Product ) ;
46+ return RedirectToPage ( "./Index" ) ;
47+ }
48+ }
49+ }
You can’t perform that action at this time.
0 commit comments