【发布时间】: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页面与Edit和Details位于同一位置,并且您已使用@page指令开始您的页面,并且您的页面指令使用正确的语法。例如:@page "{id:int}" -
@AdamVincent 谢谢。我们发现了问题,你是对的。我们必须剪切并粘贴页面指令,它具有
@page "{id:int}"而不是@page