在Razor页面.Net Core 2.1中删除的asp页中缺少asp-route-id值

时间:2018-11-06 20:05:57

标签: c# .net-core

我们有一个非常奇怪的TournamentBatch Razor Page(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();
        }



    }
}

删除Pagemodel:

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 });
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

参考:Anchor Tag Helper

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

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

还要检查并确保您使用@page指令启动了页面,并且页面指令的语法正确,并且与PageModel中的相应方法签名相匹配

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

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