T
T
Thomd2092019-05-26 12:51:46
ASP.NET
Thomd209, 2019-05-26 12:51:46

The "delete" button does not work, which should remove the candidate from the table. How to make it work?

5cea631d4e40a876121415.pngBelow is the code of two files - Index.cshtml and Index.cshtml.cs. The code for the button is provided in these two files. Also in the image attached to the question is an Index.cshtml page with a table that stores candidate information.
Index.cshtml file code:

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

    
<table class="table">
    <thead>
        <tr>
            <th scope="col">ID</th>
            <th scope="col">ФИО</th>
            <th scope="col">Дата рождения</th>
            <th scope="col">Университет</th>
            <th scope="col">Курс</th>
            <th scope="col">Факультет</th>
            <th scope="col">Контактный телефон</th>
            <th scope="col">Email</th>
            <th scope="col">О себе</th>
            <th scope="col">Дата создания</th>
            <th scope="col">Кем создано</th>
            <th scope="col">Дата изменения</th>
            <th scope="col">Кем изменено</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var candidate in Model.Candidates)
        {
        <tr>
            <td>@candidate.Id</td>
            <td>@candidate.Name</td>
            <td>@candidate.Date_of_Birth</td>
            <td>@candidate.University</td>
            <td>@candidate.University_course</td>
            <td>@candidate.Faculty</td>
            <td>@candidate.Phone_number</td>
            <td>@candidate.Email</td>
            <td>@candidate.About_myself</td>
            <td>@candidate.Date_of_creation</td>
            <td>@candidate.Creator</td>
            <td>@candidate.Date_of_change</td>
            <td>@candidate.Changer</td>
            <td>link to edit,delete button</td>
            <td>
                <a asp-page="./Edit" asp-route-id="@candidate.Id">Edit</a>
                <button type="submit" asp-page-handler="delete"
                        asp-route-id="@candidate.Id">
                    delete
                </button>
            </td>
        </tr>
        }
    </tbody>
</table>
<a asp-page="./Create">Create</a>.

Index.cshtml.cs file code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace Candidates.Pages
{
    public class IndexModel : PageModel
    {
        private readonly AppDbContext _db;

        public IndexModel(AppDbContext db)
        {
            _db = db;
        }

        public IList<Candidate> Candidates { get; set; }
        public async Task OnGetAsync()
        {
            Candidates = await _db.Candidates.AsNoTracking().ToListAsync();
        }

        public async Task<IActionResult> OnPostDeleteAsync(int Id)
        {
            var candidate = await _db.Candidates.FindAsync(Id);

            if(candidate != null)
            {
                _db.Candidates.Remove(candidate);
                await _db.SaveChangesAsync();
            }
            return RedirectToPage();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question