简体   繁体   English

如何将参数传递给 RazorPage OnGet()?

[英]How do I pass a parameter to RazorPage OnGet()?

Using razor pages I am trying to build an ecommerce website for selling clothing and I want to use once page as a template to load both mens and womens pages.使用剃刀页面我正在尝试建立一个用于销售服装的电子商务网站,我想使用一次页面作为模板来加载男士和女士页面。 To test this could be done in my _Layout.cshtml page I use <a asp-page="/Shop" asp-route-id="Mens" class="nav-link">Mens</a> and then in:为了测试这可以在我的_Layout.cshtml页面中完成,我使用<a asp-page="/Shop" asp-route-id="Mens" class="nav-link">Mens</a>然后在:

Shop.cshtml.cs商店.cshtml.cs

    [BindProperties]
public class ShopModel : PageModel
{
    public string Status { get; set; }
    public void OnGet(string gender)
    {
        switch (gender)
        {
            case "Mens":
                //get men page
                Status = gender;
                break;
            case "Womens":
                //get womens page
                break;
        }
    }
}

Shop.cshtml商店.cshtml

@page
@model ECommerce.Pages.Shop.ShopModel
@{
    ViewData["Title"] = "Shop";
}
<p>
    @Model.Status
</p>

When I debug the value of gender comes up as null , so status is never set.当我调试时,性别的值显示为null ,因此永远不会设置状态。 Am I going about this the wrong way?我会以错误的方式解决这个问题吗? Any help is much appreciated, thanks!非常感谢任何帮助,谢谢!

The parameter name is gender , not id , so it should be参数名称是gender ,而不是id ,所以应该是

<a asp-page="/Shop" asp-route-gender="Mens" class="nav-link">Mens</a>

See more about anchor tag helper's route value .查看更多关于锚标签助手的路由值

如果您要使用 OnGet 以外的其他方法,例如 OngetView,则必须将 Ver

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM