asp.net 如何从Layout.cshtml调用Razor页面C#方法

pn9klfpd  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(177)

我有一个.Net6的Razor页面应用程序,其中有一个名为Index.chshtml的页面和文件Index.chshtml.cs背后的相应代码
现在我在Index.chshtml.cs文件中有一个名为OnPostProfile()的方法,我可以从index.cshtml页面调用它,如下所示:

// index.cshtml code

 <form asp-page-handler="profile" method="post">
    <button class="btn btn-default">Profile</button>
</form> 

// code behind  (index.cshtml.cs)

public void OnPostProfile()
{
   // do stuff
}

字符串
这很好,但是我想从_layout.cshtml文件**(Navbar链接)**调用这个代码隐藏方法,因为这个方法将从多个屏幕调用。
这是我尝试过的(这些都不起作用)

<li class="nav-item">
      <a class="nav-link text-dark" asp-area="" asp-page="/Index/profile">Profile</a>
  </li> 

 <li class="nav-item">
      <a class="nav-link text-dark" asp-area="" asp-page-handler="/Index/profile">Profile</a>
  </li>


如何从_layout.cshtml(Navbar)调用此C#方法?

vktxenjb

vktxenjb1#

你可以使用下面的代码来实现它。

<li class="nav-item">
    <form id="profileForm" asp-page="/Index" asp-page-handler="profile" method="post" class="form-inline" style="display:none;">
    </form>
    <a href="javascript:void(0);" onclick="document.getElementById('profileForm').submit();" class="nav-link text-dark">Profile</a>
</li>

字符串

这是我的测试结果,稍后会分享详细代码。


的数据

  • _布局.cshtml*
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - WebApplication1</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
    <link rel="stylesheet" href="~/WebApplication1.styles.css" asp-append-version="true" />
</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">WebApplication1</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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 justify-content-between">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
                        </li>
                        <li class="nav-item">
                            <form id="profileForm" asp-page="/Index" asp-page-handler="profile" method="post" class="form-inline" style="display:none;">
                            </form>
                            <a href="javascript:void(0);" onclick="document.getElementById('profileForm').submit();" class="nav-link text-dark">Profile</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; 2023 - WebApplication1 - <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>

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

  • 索引.cshtml*
@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
<form asp-page-handler="profile" method="post">
    <button class="btn btn-default">Profile</button>
</form>

  • Index.cshtml.cs*
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebApplication1.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }

        public void OnGet()
        {

        }
        public void OnPostProfile()
        {
            Console.WriteLine("Profile handler called!");

        }
    }
}

相关问题