Dyl*_*lan 4 c# asp.net-core-mvc asp.net-core razor-pages
单击a 时,是否有更好的方法从一个 Razor 页面导航到不同的 Razor 页面button。我想在单击按钮时从 导航Index.cshtml到。Inventory.cshtml
以下是我管理的当前有效解决方案,但我不确定这是否是正确的方法。使用的参考:https ://www.jetbrains.com/dotnet/guide/tutorials/basics/razor-pages/
索引.cshtml
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<!--Page redirect-->
<form method="post" asp-page="Index">
<button type="submit" class="btn btn-cta">View Inventory</button>
</form>
Run Code Online (Sandbox Code Playgroud)
索引.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace TheInventory.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
//Page redirect on button form submit
public IActionResult OnPost()
{
return RedirectToPage("Inventory");
}
}
}
Run Code Online (Sandbox Code Playgroud)
库存.cshtml
@page
@model TheInventory.Pages.InventoryModel
@{
ViewData["Title"] = "Inventory";
}
<h1>@ViewData["Title"]</h1>
<p>This is the Inventory Page</p>
Run Code Online (Sandbox Code Playgroud)
为了解决我的问题,我使用asp-pageRazor Pages 的属性并将锚标记的href属性值设置为特定页面。
使用参考:asp.net core 锚标记帮助器 - Razor 页面
<a asp-page="/PageExample">Page Example</a>
Run Code Online (Sandbox Code Playgroud)
我将锚标记元素包装在按钮元素中,并且在单击按钮时能够导航到指定页面。
这是我的代码片段:
<button type="button" class="btn btn-cta"><a asp-page="/Inventory">Manage Inventory</a></button>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13118 次 |
| 最近记录: |