Search code examples
c#asp.net-core-mvcrazor-pagesasp.net-core-tag-helpers

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!


Solution

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