简体   繁体   中英

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:

Shop.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

@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. 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

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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