Advertisement
wingman007

2018_DotNetProgrammingPartTime_Index.cshtml.cs

Sep 25th, 2018
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. ?using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.RazorPages;
  7. using Microsoft.EntityFrameworkCore;
  8.  
  9. namespace WebApplication1bDeletMe.Pages
  10. {
  11.     public class IndexModel : PageModel
  12.     {
  13.         private readonly AppDbContext _db;
  14.  
  15.         public IList<Customer> Customers { get; set; }
  16.  
  17.         [TempData]
  18.         public string Message { get; set; }
  19.  
  20.         public IndexModel(AppDbContext db) {
  21.             _db = db;
  22.         }
  23.  
  24.         public async Task OnGetAsync()
  25.         {
  26.             Customers = await _db.Customers.AsNoTracking().ToListAsync();
  27.         }
  28.  
  29.         public async Task<ActionResult> OnPostDeleteAsync(int id)
  30.         {
  31.             var customer = await _db.Customers.FindAsync(id);
  32.             if (customer != null)
  33.             {
  34.                 _db.Customers.Remove(customer);
  35.                 await _db.SaveChangesAsync();
  36.             }
  37.             return RedirectToPage();
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement