简体   繁体   English

Asp.net 核心 -> 如何使用 pKey(invoice_id, row_id) 从表中删除行

[英]Asp.net core -> How to Delete Row from table with pKey(invoice_id, row_id)

I'm using MS SQL Server 2014, Visual Studio 2019, ASP.NET Core 5.0 with C#.我正在使用 MS SQL Server 2014、Visual Studio 2019、ASP.NET Core 5.0 和 C#。

After having created two tables - Invoice and Rows创建两个表后 - InvoiceRows

Invoice {inv_id, client, address}发票 {inv_id, 客户, 地址}
Rows {inv_id, row_id, product, price}行 {inv_id, row_id, product, price}

the "Invoice table has pkey= inv_id" “发票表有 pkey=inv_id”
the "Rows table has pkey=(inv_id,row_id)" “行表有 pkey=(inv_id,row_id)”

When I click on the delete button (on Index) it asks for two parameters but, asp-route-id permits only one id.当我单击删除按钮(在索引上)时,它要求输入两个参数,但是asp-route-id只允许一个 id。

The Delete button is:删除按钮是:

a asp-page="./Delete" asp-route-id="@item.row_id" class="btn" 

How can I solve this?我该如何解决这个问题?

asp-route-[parameter] can multiple parameters. asp-route-[parameter]可以有多个参数。 For example:例如:

<a asp-page="./Delete" asp-route-rowid="@item.row_id" asp-route-invid="@item.inv_id" class="btn">

Take the rowid and invid as the parameter in the bakend.将 rowid 和 invid 作为 bakend 中的参数。

public ActionResult Delete(int invId, int rowId)()
{

}

Here is the official introduction . 这里是官方介绍

Thank you very much to both.非常感谢你们。 I had to combine both of the answers to make it work.我必须结合这两个答案才能使其发挥作用。

This is the solution that worked for me: (lets hope it helps someone else too)这是对我有用的解决方案:(希望它也对其他人有所帮助)

Index.cshtml ==>索引.cshtml ==>

a asp-page="./Delete" asp-route-invid="@item.invId" asp-route-rowid="@item.rowId" class="btn btn-danger"一个 asp-page="./Delete" asp-route-invid="@item.invId" asp-route-rowid="@item.rowId" class="btn btn-danger"

Index.cshtml.cs ==> (Unchanged) Index.cshtml.cs ==> (不变)

Delete.cshtml ==> (Unchanged)删除.cshtml ==> (不变)

Delete.cshtml.cs ==>删除.cshtml.cs ==>

    [BindProperty]
    public InvoiceRows InvoiceRows { get; set; }

    public async Task<IActionResult> OnGetAsync(int? invid, int rowid)
    {
        if (invid < 0)
        {
            return NotFound();
        }

        InvoiceRows= await _context.InvoiceRows.Where(r => r.rowid== rowid)
            FirstOrDefaultAsync(m => m.invid== invid);

        if (InvoiceRows== null)
        {
            return NotFound();
        }
        return Page();
    }

    public async Task<IActionResult> OnPostAsync(int invid, int rowid)
    {
        if (invid< 0 || rowid< 0)
        {
            return NotFound();
        }

        InvoiceRows= await _context.InvoiceRows.FindAsync(invid, rowid);

        if (InvoiceRows!= null)
        {
            _context.InvoiceRows.Remove(InvoiceRows);
            await _context.SaveChangesAsync();
        }

        return RedirectToPage("./Index");
    }

RowsController.cs ==>行控制器.cs ==>

(Delete controller) (删除控制器)

    [Route("~/Rows/delete/{invId}/{rowId}")]
    **public async Task<ActionResult> Delete**(int invId, int rowId)
    {
        if (invId< 0 || rowId< 0)
        {
            return NotFound();
        }

        var InvoiceRows= await _context.InvoiceRows.Where(r => r.rowId== rowId)
            .FirstOrDefaultAsync(m => m.invId== invId);
        if (InvoiceRows == null)
        {
            return NotFound();
        }

        return View(InvoiceRows);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在自定义构建的 ASP.NET Core Razor Pages 可编辑网格中发布行 ID? - How do I post the row id in a custom built ASP.NET Core Razor Pages editable Grid? ASP.NET从数据列表中查找选定行的ID - ASP.NET Find id of selected row from datalist 从一个表中选择一个值以将该行的ID插入asp.net中的另一个表 - Selecting a value from one table to insert the id of the row to another one in asp.net 如何在Asp.Net Core中动态更新表的每一行 - How to update each row of a table dynamically in a Asp.Net Core 为asp.net gridview中的每一行指定一个唯一的行ID - Give a unique row Id to every row in asp.net gridview 如何从一个表中获取 Id 并将其保存到 ASP.NET Core 中的另一个表,存储库模式 - How to get an Id from one table and save it to another table in ASP.NET Core, repository pattern ASP.NET Core如何从网址获取ID - Asp.net core how to get Id from url ASP.NET Core 3.1 和 Entity Framework Core 根据 ID 从数据库中删除多条记录 - ASP.NET Core 3.1 and Entity Framework Core delete multiple records from database based on ID 如何从客户端在asp.net中添加表的删除行? - how to add delete row of table in asp.net from client side? asp.net core 如何从表行发布类别和子类别 - asp.net core how can i post category and subcategory from table row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM