简体   繁体   中英

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. My problem now is to pass the values for NodeId and BundleId from the delete button in the view.

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.

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. 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. (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.

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

 app.MapControllerRoute(name: "twoids", pattern: "BundleNodes/Delete/{n_id}/{b_id}", , defaults: new { controller = "BundleNodes", action = "Delete"} );

Easy when you know how.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM