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

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

问题描述

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

我在我的域模型中定义了一个叫做subtractProduct的方法.

在我的索引页面代码中,我定义了 IActionResult sellProduct,它在指定的 ProductId 上调用减法产品.但我不知道如何在我的 html 页面上调用这个方法.我尝试了很多组合,但似乎没有任何效果.有谁知道如何处理这个问题?非常感谢任何帮助!

我的域模型是:

公共类产品{公共 int ProductId { 获取;放;}公共整数数量{得到;放;}...public void SubtractProduct(){数量 -= 1;}}

我的索引页代码是:

公共类 IndexModel : PageModel{私有只读 CfEshop.Data.ApplicationDbContext _context;公共索引模型(CfEshop.Data.ApplicationDbContext 上下文){_context = 上下文;}公共 IList产品 { 获取;设置;}公共异步任务 OnGetAsync(){产品 = 等待 _context.Products.Include(p => p.Categories).ToListAsync();}公共 IActionResult 销售产品(int id){var 产品 = _context.Products;_context.Products.Find(id).SubtractProduct();返回页面();}}

最后是我的 Razor 页面:

@page@model CfEshop.Pages.Product.IndexModel<h2>索引</h2><table class="table"><头><tr><th>@Html.DisplayNameFor(model => model.Product[0].Quantity)</th></tr></thead>@foreach(Model.Product 中的 var 项目){<tr><td>@Html.DisplayFor(modelItem => item.Quantity)</td><td><a asp-page-handler="SellProduct" asp-route="@item.ProductId">销售产品</a></td></tr>}</tbody>

解决方案

Razor 页面具有 handler-methods,它们是 HTTP 动词.因此,要从您的页面调用方法,您需要将 On 后跟 您想要的 http 动词 然后是您的 方法名称.

例如:

public IActionResult OnGetSellProduct(int id){var 产品 = _context.Products;_context.Products.Find(id).SubtractProduct();返回页面();}

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

这是视图示例:

销售产品

<小时>

有关更多信息,请查看:

Razor Pages 简介.

Razor 页面处理程序方法.

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>

解决方案

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.

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

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