在Razor页面中调用操作 [英] Calling Action in Razor Pages

查看:36
本文介绍了在Razor页面中调用操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为Razor Pages的新手,我有一个关于从Razor Page调用方法的问题.

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.

在我的索引页代码中,我定义了IActionResult sellProduct,该产品在指定的ProductId上调用excludeProduct.但是我不知道如何在我的html页面上调用此方法.我尝试了很多组合,但似乎没有任何效果.有人知道如何处理吗?任何帮助,我们将不胜感激!

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!

我的域模型是:

public class Product

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

我的索引页代码为:

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();
    }
}

最后进入我的Razor页面:

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>

推荐答案

剃刀页面具有 handler-methods (它们是HTTP动词).因此,要从您的页面调用方法,您需要先放置 On ,然后放置所需的http动词,然后输入方法名.

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.

例如:

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

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

然后在您的视图中将名称传递给 asp-page-handler ,而不带 OnPost或OnGet 前缀或 Async 后缀.

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

这是视图示例:

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


有关更多信息,请查看以下内容:


For more information check those out:

剃刀页面简介.

剃刀页面处理程序方法.

这篇关于在Razor页面中调用操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆