在Razor page.Net Core 2.1中删除的asp页面中缺少asp路由id值



我们有一个非常奇怪的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页面(找不到页面或无法通过默认路由约定解析路由时,就会发生这种情况(您可以在这里找到更多详细信息,或者如何自定义约定

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

还要检查您是否已使用@page指令启动页面,以及页面指令的语法是否正确,是否与PageModel中的适当方法签名相匹配

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

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

最新更新