简体   繁体   English

将两个或多个参数从 model 视图传递到 EF Core / ASP.NET Core MVC 应用程序中的 controller

[英]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.我现在的问题是从视图中的删除按钮传递NodeIdBundleId的值。

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.

相关问题 ASP NET Core (MVC) 将参数从视图传递到控制器的问题 - ASP NET Core (MVC) problem with passing parameters from the view to the controller ASP.NET 核心 MVC - 将 Model 数据从视图传递回 Controller - ASP.NET Core MVC - Passing Model Data Back to Controller from View c# asp.net core mvc 将数据从视图传递到控制器并从控制器返回到视图 - c# asp.net core mvc passing data from view to controller and from controller back to view 如何将两个参数传递给 ASP.NET Core MVC 中的 controller? - How to pass two parameters to a controller in ASP.NET Core MVC? ASP.NET Core 6 MVC - EF Core 6 - model 验证不正确 - ASP.NET Core 6 MVC - EF Core 6 - model is not validating correctly 为什么我的 ASP.NET Core 3.1 控制器会自动为从视图返回的 EF Core 模型分配 ID? - Why is my ASP.NET Core 3.1 controller automatically assigning an ID to an EF Core model that was returned from a view? ASP.net Core MVC 2.0将FormEditModel从视图传递到控制器 - ASP.net Core MVC 2.0 Passing FormEditModel from view to controller ASP.NET Core MVC:编辑用户,将数据从视图传递到 controller 时出现问题,身份 - ASP.NET Core MVC : edit user, problem with passing data from view to controller ,identity ASP.NET MVC Core TextBoxFor event pass model from View to Controller - ASP.NET MVC Core TextBoxFor event pass model from View to Controller Model 列表返回为 null 从视图到 controller Z9E0DA8438E1E38A1C30F4B76ZCE3 核心 - Model List returning as null from view to controller ASP.NET Core MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM