[英]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创建两个表后 - Invoice和Rows
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)()
{
}
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.