【问题标题】:Missing asp-route-id value in asp-page for Delete in Razor Page .Net Core 2.1在 Razor Page .Net Core 2.1 中删除的 asp-page 中缺少 asp-route-id 值
【发布时间】:2019-04-10 06:39:24
【问题描述】:

我们有一个非常奇怪的 TournamentBatch Razor 页面 (index.cshtml),我们有这样的东西:

<td>
                <a asp-page="/TournamentBatchItems/Index" asp-route-id="@item.TournamentBatchID">Items</a> |
                <a asp-page="./Edit" asp-route-id="@item.TournamentBatchID">Edit</a> |
                <a asp-page="./Details" asp-route-id="@item.TournamentBatchID">Details</a> |
                <a asp-page="./Delete" asp-route-id="@item.TournamentBatchID">Delete</a>
            </td>

当我们运行这个并结束时,页面上没有返回仅用于 /Delete 链接的 id,其他链接正常(/TournamentBatchItems/Index、/Edit、/Details)。

这是html源码的样子:

<td>
                <a href="/TournamentBatchItems?id=5088415a-f491-4438-1aa9-08d642f7dffe">Items</a> |
                <a href="/TournamentBatches/Edit?id=5088415a-f491-4438-1aa9-08d642f7dffe">Edit</a> |
                <a href="/TournamentBatches/Details?id=5088415a-f491-4438-1aa9-08d642f7dffe">Details</a> |
                <a href="">Delete</a> |
            </td>

现在删除的其他页面只有这个页面可以。 ?!?!

有什么想法吗?

索引页面模型:

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;
using AthlosifyWebArchery.Data;
using AthlosifyWebArchery.Models;
using AthlosifyWebArchery.Utilities;
using CsvHelper;
using System.IO;

namespace AthlosifyWebArchery.Pages.TournamentBatches
{
    public class IndexModel : PageModel
    {
        private readonly AthlosifyWebArchery.Data.ApplicationDbContext _context;

        public IndexModel(AthlosifyWebArchery.Data.ApplicationDbContext context)
        {
            _context = context;
        }

        public IList<TournamentBatch> TournamentBatches { get;set; }

        public async Task OnGetAsync()
        {
            TournamentBatches = await _context.TournamentBatch.ToListAsync();
        }



    }
}

删除页面模型:

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;
using AthlosifyWebArchery.Data;
using AthlosifyWebArchery.Models;

namespace AthlosifyWebArchery.Pages.TournamentBatches
{
    public class DeleteModel : PageModel
    {
        private readonly AthlosifyWebArchery.Data.ApplicationDbContext _context;

        public DeleteModel(AthlosifyWebArchery.Data.ApplicationDbContext context)
        {
            _context = context;
        }

        [BindProperty]
        public TournamentBatch TournamentBatch { get; set; }
        public string ConcurrencyErrorMessage { get; set; }

        public async Task<IActionResult> OnGetAsync(Guid? id, bool? concurrencyError)
        {
            if (id == null)
            {
                return NotFound();
            }

            TournamentBatch = await _context.TournamentBatch
                                        .AsNoTracking() //Addded
                                        .FirstOrDefaultAsync(m => m.TournamentBatchID == id);

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

            if (concurrencyError.GetValueOrDefault())
            {
                ConcurrencyErrorMessage = "The record you attempted to delete "
                  + "was modified by another user after you selected delete. "
                  + "The delete operation was canceled and the current values in the "
                  + "database have been displayed. If you still want to delete this "
                  + "record, click the Delete button again.";
            }

            return Page();
        }

        public async Task<IActionResult> OnPostAsync(Guid? id)
        {
            /*if (id == null)
            {
                return NotFound();
            }

            TournamentBatch = await _context.TournamentBatch.FindAsync(id);

            if (TournamentBatch != null)
            {
                _context.TournamentBatch.Remove(TournamentBatch);
                await _context.SaveChangesAsync();
            }

            return RedirectToPage("./Index");
            */

            try
            {
                if (await _context.TournamentBatch.AnyAsync(
                    m => m.TournamentBatchID == id))
                {
                    // Department.rowVersion value is from when the entity
                    // was fetched. If it doesn't match the DB, a
                    // DbUpdateConcurrencyException exception is thrown.
                    _context.TournamentBatch.Remove(TournamentBatch);
                    await _context.SaveChangesAsync();
                }
                return RedirectToPage("./Index");
            }
            catch (DbUpdateConcurrencyException)
            {
                return RedirectToPage("./Delete",
                    new { concurrencyError = true, id = id });
            }
        }
    }
}

【问题讨论】:

  • 您需要从删除页面模型中发布代码。
  • 但是链接甚至没有在索引页面上生成?为什么它与删除页面模型有关?无论如何,我已经添加了删除页面模型
  • 这通常发生在锚标签助手(在本例中为asp-page)找不到页面或没有路由匹配时。检查并确保Delete.cshtml 页面与EditDetails 位于同一位置,并且您已使用@page 指令开始您的页面,并且您的页面指令使用正确的语法。例如:@page "{id:int}"
  • @AdamVincent 谢谢。我们发现了问题,你是对的。我们必须剪切并粘贴页面指令,它具有@page "{id:int}" 而不是@page

标签: c# .net-core


【解决方案1】:

参考:Anchor Tag Helper

这通常发生在锚标签助手(在本例中为 asp-page)找不到页面或无法通过默认路由约定解析路由时(您可以找到更多详细信息,或如何自定义约定here

首先检查并确保您的 Delete.cshtml 页面与 Edit.cshtmlDetails.cshtml 位于同一位置(因为它们正在工作,并且您对所有 3 个使用相同的相对路径。)

还要检查您是否已使用 @page 指令开始您的页面,并且您的页面指令的语法是否与您的 PageModel 中的适当方法签名相匹配

示例:@page "{id:int}"

public IActionResult OnGet(int id) 
{
...
}

【讨论】:

    猜你喜欢
    • 2021-07-04
    • 2019-01-03
    • 2020-08-08
    • 2019-01-16
    • 2021-07-17
    • 1970-01-01
    • 2019-02-20
    • 2019-04-03
    • 2021-03-22
    相关资源
    最近更新 更多