Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Action in Razor Pages

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>
like image 919
xcelm Avatar asked Dec 22 '18 10:12

xcelm


People also ask

How do you call an action in Razor page?

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 . And in your view pass the name to the asp-page-handler without the OnPost or OnGet prefix or Async suffix.

How do you handle a click event in Razor pages?

An anchor tag helper can be used to specify the name of the click event handler. For this the attribute "asp-page-handler" is set equal to the name of the function in the backing class.

How do I redirect in Razor pages?

You can use the IActionResult to return a redirection or your razor page.


1 Answers

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.

like image 188
Hameed Avatar answered Oct 22 '22 21:10

Hameed