Calling Action in Razor Pages

24,779

Razor pages have handler-methods which are HTTP verbs. So to call a method from your page you need to put On followed by the http verb you want then your method name.

E.g.:

public IActionResult OnGetSellProduct(int id)
{
    var products = _context.Products;

    _context.Products.Find(id).SubtractProduct();
    return Page();
}

And in your view pass the name to the asp-page-handler without the OnPost or OnGet prefix or Async suffix.

Edit: here is the view sample:

<a asp-page-handler="SellProduct" asp-route-id="@item.ProductId">Sell Product</a>

For more information check those out:

Introduction to Razor Pages.

Razor Pages Handler Methods.

Share:
24,779
xcelm
Author by

xcelm

Currently Data analyst aspiring to become ASP.NET developer

Updated on August 18, 2021

Comments

  • xcelm
    xcelm over 2 years

    as a newbie to Razor Pages I have a question regarding calling methods from Razor Page.

    I have a method called subtractProduct defined in my domain model.

    In my Index Page code I define IActionResult sellProduct which calls subtractProduct on specified ProductId. But I have no clue how to call this method on my html page. I've tried a lot of combinations, but nothing seems to be working. Anyone knows how to deal with this? Any help is greatly appreciated!

    My domain model is:

    public class Product
    
    {
        public int ProductId { get; set; }
        public int Quantity { get; set; }   
        ...
        public void SubtractProduct()
        {
            Quantity -= 1;
        }
    }
    

    My Index Page code is:

    public class IndexModel : PageModel
    {
        private readonly CfEshop.Data.ApplicationDbContext _context;
    
        public IndexModel(CfEshop.Data.ApplicationDbContext context)
        {
            _context = context;
        }
    
        public IList<Models.Product> Product { get;set; }
    
        public async Task OnGetAsync()
        {
            Product = await _context.Products
                .Include(p => p.Categories).ToListAsync();
        }
    
        public IActionResult sellProduct(int id)
        {
            var products = _context.Products;
    
            _context.Products.Find(id).SubtractProduct();
            return Page();
        }
    }
    

    And finaly my Razor page:

    @page
    @model CfEshop.Pages.Product.IndexModel
    <h2>Index</h2>
    
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Product[0].Quantity)
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Product)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Quantity)
                    </td>
                    <td>
                        <a asp-page-handler="SellProduct" asp-route="@item.ProductId">Sell Product</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>