[英]Passing two or more parameters from model view to controller in EF Core / ASP.NET Core MVC application
When I _context.Controller.Remove(tbundle)
where tbundle is the following I get a successful delete from the table.当我
_context.Controller.Remove(tbundle)
其中 tbundle 如下时,我从表中成功删除。 My problem now is to pass the values for NodeId
and BundleId
from the delete button in the view.我现在的问题是从视图中的删除按钮传递
NodeId
和BundleId
的值。
BundleNode tbundle = _context.BundleNodes.Where(a => a.NodeId == 707955 && a.BundleId == 11).Single();
So I think I need to modify this line of code:所以我想我需要修改这行代码:
public async Task<IActionResult> DeleteConfirmed(int id)
such that it will accept two parameters:这样它将接受两个参数:
I tried this:我试过这个:
public async Task<IActionResult> DeleteConfirmed(int NodeId, int BundleId)
but it doesn't work.但它不起作用。 Maybe that is the correct syntax but I'm not sure how to structure the URL in the view such that the controller will consume it correctly.
也许这是正确的语法,但我不确定如何在视图中构造 URL 以便 controller 正确使用它。
The delete button code in the view is:视图中的删除按钮代码为:
<td style="text-align: center"> <a asp-action="Delete" asp-route-id="?n_id=@item.NodeId&b_id=@item.BundleId"> <i class="fas fa-trash" style="color:red"></i> </a> </td>
When I hover over the delete button, the URL (bottom left) looks like this: "https://localhost:44324/BundleNodes/Delete/?n_id=739938&b_id=11" which is exactly what it's meant to look like.当我在删除按钮上使用 hover 时,URL(左下角)看起来像这样:“https://localhost:44324/BundleNodes/Delete/?n_id=739938&b_id=11”这正是它的意思。 If I paste that URL into the browser then the n_id and b_id variables are populated correctly and everything works as it should.
如果我将 URL 粘贴到浏览器中,则 n_id 和 b_id 变量会正确填充,并且一切正常。 (FYI, if I right clock and copy the link and paste that into the browser it looks like this:"https://localhost:44324/BundleNodes/Delete/%3Fn_id%3D910699%26b_id%3D11). If I click the delete button then both n_id and b_id are null and of course the delete fails because there is nothing to delete.
(仅供参考,如果我正确时钟并复制链接并将其粘贴到浏览器中,它看起来像这样:“https://localhost:44324/BundleNodes/Delete/%3Fn_id%3D910699%26b_id%3D11)。如果我点击删除按钮然后 n_id 和 b_id 都是 null ,当然删除失败,因为没有什么可以删除。
Found a solution.找到了解决方案。 For the button in the view:
对于视图中的按钮:
@{ var link1 = Url.RouteUrl("twoids", new { controller = "BundleNodes", Action = "Delete", n_id = item.NodeId, b_id = item.BundleId }); } <a href="@link1"> <i class="fas fa-trash" style="color:red"></i></a>
and a custom route in Program.cs和 Program.cs 中的自定义路由
app.MapControllerRoute(name: "twoids", pattern: "BundleNodes/Delete/{n_id}/{b_id}", , defaults: new { controller = "BundleNodes", action = "Delete"} );
Easy when you know how.当你知道怎么做时很容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.