Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 610
steve Avatar asked Oct 30 '25 06:10

steve


1 Answers

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.

like image 73
Luke Vo Avatar answered Oct 31 '25 20:10

Luke Vo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!