简体   繁体   English

在 Razor Page .Net Core 2.1 中删除 asp-page 中缺少 asp-route-id 值

[英]Missing asp-route-id value in asp-page for Delete in Razor Page .Net Core 2.1

We have this very weird TournamentBatch Razor Page (index.cshtml) where we have something like this:我们有一个非常奇怪的 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>

and when we are running this and end up that there is no id returned on the page just for /Delete link and other links are ok (/TournamentBatchItems/Index, /Edit, /Details).当我们运行它并最终发现页面上没有仅针对 /Delete 链接返回的 id 并且其他链接正常(/TournamentBatchItems/Index、/Edit、/Details)。

This is the html source looks like:这是 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>

Now other pages for Delete are OK only this page.现在其他删除页面都可以,只有这个页面。 ?!?! ?!?!

Any ideas?有任何想法吗?

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



    }
}

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

Reference: Anchor Tag Helper参考: 锚标签助手

Usually this happens when the anchor tag helper (in this case, asp-page) can't find the page or can't resolve the route through the default route conventions (you can find more details, or how to customize the conventions here通常当锚标记助手(在这种情况下,asp-page)找不到页面或无法通过默认路由约定解析路由(您可以找到更多详细信息,或如何在此处自定义约定)时,会发生这种情况

First check and make sure your Delete.cshtml page is in the same spot as Edit.cshtml and Details.cshtml (since they are working, and you used the same relative path for all 3.)首先检查并确保您的Delete.cshtml页面与Edit.cshtmlDetails.cshtml位于同一位置(因为它们正在工作,并且您对所有 3 个页面使用了相同的相对路径。)

Also check and that you've started your page with the @page directive, and that your page directive is in the correct syntax which matches the appropriate method signature in your PageModel还要检查您是否已使用@page指令启动页面,并且您的页面指令的语法是否与PageModel中的适当方法签名匹配

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net 核心和 asp-route-id - asp.net core and asp-route-id 如何将属性从剃刀页面模型传递给 asp-route-id 标签助手 - How do I pass a property from a razor page model to asp-route-id tag helper .net 核心 asp-route-id 标签助手,带多变量 id - .net core asp-route-id tag helper with multivariable id ASP-PAGE 不重定向。 ASP NET 核心 MVC 3.1 - ASP-PAGE not redirecting. ASP NET CORE MVC 3.1 ASP.NET Core 2.1 Razor 页面返回带模型的页面 - ASP.NET Core 2.1 Razor page return Page with model 因为我们可以通过 asp-route-id 将 integer 从 razor 页面传递到 controller,所以我们如何传递字符串呢? - As we can pass integer from razor page to controller by asp-route-id , how can we pass a string instead? 使用Razor页面更改ASP.Net Core 2.1中的主页 - Changing home page in ASP.Net Core 2.1 with Razor pages Asp.Net Core Razor Pages: asp-route to use value from input on the same page - Asp.Net Core Razor Pages: asp-route to use value from input on same page 如何在 ASP .NET Core 2.1 中将登录页面设为默认路由? - How to make Login page as a default route in ASP .NET Core 2.1? ASP.NET Core RazorPages 强制 AnchorTagHelper (asp-page) 使用小写路由 - ASP.NET Core RazorPages to force AnchorTagHelper (asp-page) to use lowercase routes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM