在ASP.NET Core Razor Pages中的删除功能

admin
admin
2022-03-04
分享:

在ASP.NET Core Razor Pages中的删除功能

导航:

在本视频中,我们将讨论如何在ASP.NET Core Razor Pages项目中实现Delete操作。让我们通过一个例子来理解这一点。

Razor Pages删除示例

单击“删除”按钮后,重定向用户以删除确认页面

删除确认页面应如下图所示,使用确认和取消按钮。

IStudentRepository.cs

包括Delete()方法,该方法按ID删除。已删除的学生对象将返回。


public interface IStudentRepository
{
    IEnumerable<Student> GetAllStudents();
    Student GetStudent(int id);
    Student Update(Student updatedStudent);
    Student Add(Student newStudent);
    Student Delete(int id);
}

MockStudentRepository.cs


public class MockStudentRepository : IStudentRepository
{
    private List<Student> _studentList;

      public MockStudentRepository()
        {
            _studentList = new List<Student>()
            {
            new Student() { Id = 1, Name = "张三", Major = MajorEnum.ComputerScience, Email = "Tony-zhang@52abp.com" },
            new Student() { Id = 2, Name = "李四", Major = MajorEnum.ElectronicCommerce, Email = "lisi@52abp.com" },
            new Student() { Id = 3, Name = "王二麻子", Major = MajorEnum.Mathematics, Email = "wang@52abp.com" },
            };
        }

       public Student Delete(int id)
        {
            var studentToDelete = _studentList.FirstOrDefault(e => e.Id == id);

            if (studentToDelete != null)
            {
                _studentList.Remove(studentToDelete);
            }

            return studentToDelete;
        }

    // 其他代码
}

Delete.cshtml.cs


namespace StudentManagement.RazorPage.Pages.Students
{
    public class DeleteModel : PageModel
    {
        private readonly IStudentRepository studentRepository;

        public DeleteModel(IStudentRepository studentRepository)
        {
            this.studentRepository = studentRepository;
        }

        [BindProperty]
        public Student Student { get; set; }

        public IActionResult OnGet(int id)
        {
            Student = studentRepository.GetStudent(id);

            if (Student == null)
            {
                return RedirectToPage("/NotFound");
            }

            return Page();
        }

        public IActionResult OnPost()
        {
            Student deletedStudent = _studentRepository.Delete(Student.Id);

            if (deletedStudent == null)
            {
                return RedirectToPage("/NotFound");
            }

            return RedirectToPage("Index");
        }
    }
}

Delete.cshtml


@page "{id}"
@model StudentManagement.RazorPage.Pages.Students.DeleteModel
@{
    ViewData["Title"] = "删除";
}

<h1>确认删除</h1>

<div class="alert alert-danger">
    <h5>确实要删除学生 - @Model.Student.Name  信息吗?</h5>
    <form method="post">
        <button type="submit" class="btn btn-danger">确认</button>
        <a asp-page="/Students/Index" class="btn btn-primary">取消</a>
    </form>
</div>

Index.cshtml

在学生列表页面(Students/Index.cshtml)上包含以下“删除”按钮。单击按钮后,请求将重定向到“删除 Razor ”页面,并向其传递要删除的学生的ID作为路由参数。

                <a asp-page="/Students/Delete"  asp-route-ID="@student.Id" class="btn btn-danger m-1">删除</a>