title: Trying to write CRUD fucntions for ASP .NET Core razor pages. Why doesn't my delete function work?
body: <p><a href="https://github.com/double-happ1ness/chinook" rel="nofollow noreferrer">GitHub</a></p>
  <p><strong>Delete.cshtml.cs</strong></p>
  <pre><code>#region snippet_All
  using Project.Models; //using ContosoUniversity.Models;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.AspNetCore.Mvc.RazorPages;
  using Microsoft.EntityFrameworkCore;
  using System.Threading.Tasks;
  
  namespace Project.Pages.Albums //ContosoUniversity.Pages.Students
  {
      public class DeleteModel : PageModel
      {
          private readonly Chinook _context;
  
          public DeleteModel(Chinook context)
          {
              _context = context;
          }
  
          [BindProperty]
          public Album Album { get; set; }
          public string ErrorMessage { get; set; }
  
          public async Task&lt;IActionResult&gt; OnGetAsync(int? id, bool? saveChangesError = false)
          {
              if (id == null)
              {
                  return NotFound();
              }
  
              Album = await _context.Albums
                  .AsNoTracking()
                  .FirstOrDefaultAsync(m =&gt; m.AlbumId == id);
  
              if (Album == null)
              {
                  return NotFound();
              }
  
              if (saveChangesError.GetValueOrDefault())
              {
                  ErrorMessage = &quot;Delete failed. Try again&quot;;
              }
  
              return Page();
          }
  
          public async Task&lt;IActionResult&gt; OnPostAsync(int? id)
          {
              if (id == null)
              {
                  return NotFound();
              }
  
              var album = await _context.Albums.FindAsync(id);
  
              if (album == null)
              {
                  return NotFound();
              }
  
              try
              {
                  _context.Albums.Remove(album);
                  await _context.SaveChangesAsync();
                  return RedirectToPage(&quot;./Index&quot;);
              }
              catch (DbUpdateException /* ex */)
              {
                  //Log the error (uncomment ex variable name and write a log.)
                  return RedirectToAction(&quot;./Delete&quot;,
                                       new { id, saveChangesError = true });
              }
          }
      }
  }
  #endregion
  </code></pre>
  <p><strong>Delete.cshtml</strong></p>
  <pre><code>@page
  @model Project.Pages.Albums.DeleteModel
  
  @{
      ViewData[&quot;Title&quot;] = &quot;Delete&quot;;
  }
  
  &lt;h1&gt;Delete&lt;/h1&gt;
  
  &lt;p class=&quot;text-danger&quot;&gt;@Model.ErrorMessage&lt;/p&gt;
  
  &lt;h3&gt;Are you sure you want to delete this?&lt;/h3&gt;
  &lt;div&gt;
      &lt;h4&gt;Album&lt;/h4&gt;
      &lt;hr /&gt;
      &lt;dl class=&quot;row&quot;&gt;
          &lt;dt class=&quot;col-sm-2&quot;&gt;
              @Html.DisplayNameFor(model =&gt; model.Album.Title)
          &lt;/dt&gt;
          &lt;dd class=&quot;col-sm-10&quot;&gt;
              @Html.DisplayFor(model =&gt; model.Album.Title)
          &lt;/dd&gt;
          &lt;dt class=&quot;col-sm-2&quot;&gt;
              @Html.DisplayNameFor(model =&gt; model.Album.ArtistId)
          &lt;/dt&gt;
          &lt;dd class=&quot;col-sm-10&quot;&gt;
              @Html.DisplayFor(model =&gt; model.Album.ArtistId)
          &lt;/dd&gt;
          &lt;dt class=&quot;col-sm-2&quot;&gt;
              @Html.DisplayNameFor(model =&gt; model.Album.AlbumId)
          &lt;/dt&gt;
          &lt;dd class=&quot;col-sm-10&quot;&gt;
              @Html.DisplayFor(model =&gt; model.Album.AlbumId)
          &lt;/dd&gt;
      &lt;/dl&gt;
  
      &lt;form method=&quot;Delete&quot;&gt;
          &lt;input type=&quot;hidden&quot; asp-for=&quot;AlbumId&quot; /&gt;
          &lt;input type=&quot;submit&quot; value=&quot;Delete&quot; class=&quot;btn btn-danger&quot; /&gt; |
          &lt;a asp-page=&quot;./Index&quot;&gt;Back to List&lt;/a&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  <p><strong>Album.cshtml</strong></p>
  <pre><code>@page
  @model Project.Pages.AlbumsModel            
  @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
  
  &lt;div class=&quot;row&quot;&gt;
      &lt;h1 class=&quot;display-2&quot;&gt;Albums&lt;/h1&gt;
      &lt;table class=&quot;table&quot;&gt;
          &lt;thead class=&quot;thead-inverse&quot;&gt;
              &lt;tr&gt;
                  &lt;th&gt;Album ID, artist ID, Album title&lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
              @foreach (string album in Model.Albums)
              {
                  &lt;tr&gt;
                      &lt;td&gt;@album&lt;/td&gt;
                      &lt;td&gt;&lt;a asp-page=&quot;./Delete&quot; asp-route-id=&quot;@album&quot;&gt;Delete&lt;/a&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              }
          &lt;/tbody&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  &lt;div class=&quot;row&quot;&gt;
      &lt;p&gt;Enter a title &amp; ArtistID for a new album:&amp;nbsp;&lt;/p&gt;
      &lt;form method=&quot;POST&quot;&gt;
          &lt;div&gt;&lt;input asp-for=&quot;Album.Title&quot; /&gt;&lt;/div&gt;
          &lt;div&gt;&lt;input asp-for=&quot;Album.ArtistId&quot; /&gt;&lt;/div&gt;
          &lt;input type=&quot;submit&quot; /&gt;
      &lt;/form&gt;
          @* &lt;p&gt;Enter a value and Album ID for a new album title:&amp;nbsp;&lt;/p&gt;
      &lt;form method=&quot;POST&quot;&gt;
          &lt;div&gt;&lt;input asp-for=&quot;Album.Title&quot; /&gt;&lt;/div&gt;
          &lt;div&gt;&lt;input asp-for=&quot;Album.AlbumId&quot; /&gt;&lt;/div&gt;
          &lt;input type=&quot;submit&quot; /&gt;
      &lt;/form&gt; *@
  &lt;/div&gt;
  </code></pre>
  <p><strong>Album.cshtml.cs</strong></p>
  <pre><code>using Microsoft.AspNetCore.Mvc.RazorPages;
  using System.Collections.Generic;
  using System.Linq;
  using Microsoft.AspNetCore.Mvc;
  using Project.Models;
  using System;
  
  namespace Project.Pages
  {
      public class AlbumsModel : PageModel
      {
          private Chinook db;
  
          public AlbumsModel(Chinook injectedContext)
          {
              db = injectedContext;
          }
          public IEnumerable&lt;string&gt; Albums { get; set; }
          public void OnGet()
          {
              ViewData[&quot;Title&quot;] = &quot;Chinook Web Site - Albums&quot;;
              // Albums = db.Albums.Select(s =&gt; s.Title);
              Albums = db.Albums.Select(s =&gt; s.AlbumId.ToString() + &quot;. &quot; + s.ArtistId + &quot;. &quot; + s.Title);
          }
          [BindProperty]
          public Album Album { get; set; }
  
          public IActionResult OnPost()
          {
              if (ModelState.IsValid)
              {
                  db.Albums.Add(Album);
                  db.SaveChanges();
                  return RedirectToPage(&quot;/albums&quot;);
              }
              return Page();
          }
  
          // void RemoveAlbum(int albumId)
          // {
          //     var album = db.Albums.Find(albumId);
          //     if (album == null)
          //         throw new ArgumentOutOfRangeException();
          //     db.Albums.Remove(album);
          //     db.SaveChanges();
          // }
  
          public IActionResult DeleteAlbum(int AlbumId)
          {
              var album = db.Albums.Find(AlbumId);
  
              if (album == null) return Page();
  
              db.Albums.Remove(album); db.SaveChanges(); return RedirectToPage(&quot;/albums&quot;);
          }
      }
  }
  </code></pre>
  <p>My project builds and runs fine, but the Delete button does nothing but refresh the page. I've tried reloading the page, but that makes no odds. I'm not convinced <code>&lt;td&gt;&lt;a asp-page=&quot;./Delete&quot; asp-route-id=&quot;@album&quot;&gt;Delete&lt;/a&gt;&lt;/td&gt;</code> is right, but when I try to plug the <code>AlbumId</code> in there instead of just <code>album</code>, I always get errors. Any help please? TIA.</p>