如何将 属性 从剃刀页面模型传递给 asp-route-id 标签助手

How do I pass a property from a razor page model to asp-route-id tag helper

所以我正在使用 asp.net razor 页面,我想从我的一个页面模型中传递一个 属性 以用于 asp-route-id 标签助手_layout 页面。我尝试将特定页面模型注入 _layout 页面并通过 @Model 标记访问它,当我使用它并将鼠标悬停在导航栏上时,我想显示的路线 ID 出现了,但是当我点击它时我得到了这个错误:

如果我使用 viewdata,它会在单击导航项后消失并变为空。

_layout.cshtml(共享)

@model VescovererWebApp.Pages.AccountModel


<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - VescovererWebApp</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                <a class="navbar-brand" asp-area="" asp-page="/Index"><img id="logo" src="~/Logo/vescoverer.png" /></a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                    <partial name="_LoginPartial" />
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a id="cu" class="nav-link text-dark" asp-area="" asp-page="/Account" asp-route-id="@Model.currentUserID">Account</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Vescover" asp-route-id="@Model.currentUserID">Vescover</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Vescovered" asp-route-id="@Model.currentUserID">Vescovered</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Verify" asp-route-id="@Model.currentUserID">Verify</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2021 - VescovererWebApp - <a asp-area="" asp-page="/Privacy">Privacy</a>
        </div>
    </footer>

    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>

    @RenderSection("Scripts", required: false)
</body>
</html>

<style>
    #logo {
        height: 40px;
        width: 40px;
    }
</style>

尝试使用 AccountModel 作为基础 class

public class VescoveredModel : AccountModel

或者您可以创建接口 IAccountModel

public class AccountModel:IAccountModel

public class VescoveredModel : IAccountModel

并更改布局模型

@model VescovererWebApp.Pages.IAccountModel