asp.net 删除功能不起作用,如何删除?

sqxo8psd  于 2023-02-06  发布在  .NET
关注(0)|答案(2)|浏览(171)

我目前正在codeacademy.com学习RazorPages。我做了视频教程中显示的所有事情,不幸的是它不起作用:
本项目的任务:“数据删除当前项目在索引页列表上有一个按钮,用于删除当前洲或国家。该按钮将被修改为调用离散的Delete.cshtml页。该页将显示当前记录以供查看,并提供一个”删除“按钮。一旦删除发生,用户将被重定向回列表,以便他们获得任务成功的视觉反馈。
代码和标记很容易从现有Detail.cshtml页复制。复制该页后,我们添加一个删除按钮,并从Index.cshtml.cs页中的OnPostAsync()方法复制必要的语句。”已创建删除页。问题是:当我按下按钮删除页面上的删除,我有重定向到浏览器中的此链接:
https://本地主机/大陆/删除/SA?大陆. ID = SA
实际上没有发生删除没有重定向
可能是什么错误?
代码删除.cshtml:

@page "{id}"
@model DeleteModel
@{
  ViewData["Title"] = "Continent Delete";
}

<div class="jumbotron p-3">
  <div class="d-flex align-items-center">
    <h1 class="display-4 flex-grow-1">
      Continent Delete
    </h1>
    <a class="btn btn-primary btn-sm" asp-page="./Index">
      Back to List
    </a>
  </div>
</div>
[enter image description here](https://i.stack.imgur.com/tFnrX.jpg)
<div class="d-flex">
  <div class="p-2 bg-primary text-white text-right" style="flex:0 0 15%">
    @Html.DisplayNameFor(model => model.Continent.ID)
  </div>
  <div class="p-2 border-top border-right border-bottom border-primary" style="flex:1 0 auto">
    @Html.DisplayFor(model => model.Continent.ID)
  </div>
</div>
<div class="d-flex">
  <div class="p-2 bg-primary text-white text-right" style="flex:0 0 15%">
    @Html.DisplayNameFor(model => model.Continent.Name)
  </div>
  <div class="p-2 border-right border-bottom border-primary" style="flex:1 0 auto">
    @Html.DisplayFor(model => model.Continent.Name)
  </div>
</div>

<form metod="post" class="d-flex flex-row-reverse mt-3">
  <input type="hidden" asp-for="Continent.ID"/>
  <input type="submit" value="Delete" class="btn btn-danger btn-sm"/>
</form>

Delete.cshtml.cs:

using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using RazorCountry.Models;
using RazorCountry.Data;

namespace RazorCountry.Pages.Continents
{
  public class DeleteModel : PageModel
  {
    private readonly CountryContext _context;

    public DeleteModel(CountryContext context)
    {
      _context = context;
    }

    public Continent Continent { get; set; }

    public async Task<IActionResult> OnGetAsync(string id)
    {
      Continent = await _context.Continents
        .Include(c => c.Countries)
        .AsNoTracking()
        .FirstOrDefaultAsync(m => m.ID == id);

      if (Continent == null)
      {
        return NotFound();
      }

      return Page();
    }
    public async Task<IActionResult> OnPostAsync(string id)
    {
      if (id == null)
      {
        return NotFound();
      }
      // Find the continent
      Continent Continent = await _context.Continents.FindAsync(id);
      //Delete the continent
      if (Continent != null)
      {
        _context.Continents.Remove(Continent);
      }
      //Persist Changes
      await _context.SaveChangesAsync();
      //Redirect to the user
      return RedirectToPage("./Index");
    }
  }
}

模拟洲.cs:

using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;

namespace RazorCountry.Models
 {
public class Continent
  {
[Required, StringLength(2, MinimumLength = 2), Display(Name = "Code")]
[RegularExpression(@"[A-Z]+", ErrorMessage = "Only upper case characters are allowed.")]
public string ID { get; set; }

[Required]
public string Name { get; set; }

public ICollection<Country> Countries { get; set; }
  }
 }

尝试了解RazorPages的功能如何工作,尝试以正确的方式修复错误

i2byvkas

i2byvkas1#

没有发生错误的原因是因为没有命中OnPostAsync方法。您在表单中拼错了method属性(您有metod),因此提交它会生成默认的GET请求,并执行OnGetAsync处理程序。
纠正了该错误后,您仍然有一个未解决的问题。
您的OnPostAsync处理程序需要一个名为id的参数。窗体中的隐藏域标记帮助器生成一个名为Continent.ID的参数。因此,该值将不会绑定到id参数。最简单的解决方案是取消Delete页面底部表单中的标记帮助器,并将其替换为带有name的普通HTML输入属性:

<form method="post" class="d-flex flex-row-reverse mt-3">
  <input type="hidden" name="id" value="@Continent.ID"/>
  <input type="submit" value="Delete" class="btn btn-danger btn-sm"/>
</form>

您可以在这里阅读更多关于模型绑定的信息:https://www.learnrazorpages.com/razor-pages/model-binding

n9vozmp4

n9vozmp42#

多亏了@MikeBrind,我终于改了代码

<form method="post" class="d-flex flex-row-reverse mt-3">
  <input type="hidden" name="id" value="@Model.Continent.ID"/>
  <input type="submit" value="Delete" class="btn btn-danger btn-sm"/>
</form>

而且很管用!

相关问题