将多个参数传递给控制器​​操作

时间:2018-01-06 19:52:54

标签: c# asp.net-core routes

要访问资源,我必须传递两个ID,因为两个ID构建了密钥。

operator new

我在控制器操作中获得std::list个以下签名:

@Html.ActionLink("Details", "Details", new { id = item.CallerId, customerId = item.CustomerId })

我使用默认路由。我该怎么做才能传递两个ID?

2 个答案:

答案 0 :(得分:1)

有多种方法可以完成这项工作。其中一种方法是构建一个ViewModel以包含两个ID。另一种方法是使用HttpGetAttribute来指定路线:

[HttpGet("Details/{id}/{customerId}")]
public async Task<IActionResult> Details(int? id, int? customerId)

此处,{id}将映射到id{customerId}customerId,前提是收到的值为int

答案 1 :(得分:0)

您必须定义第二个ID的来源,可以来自标头,查询参数或路由参数。 正如您所见,默认路由将路由定义为/{controller}/{action}/{?id},它告诉MVC从路径获取id的位置。

在您的情况下,我建议将这两个ID作为查询参数发送。

此链接可能对您如何将参数作为查询参数传递有用。