pw94
pw94

Reputation: 401

Pass multiple parameters to the controller action

To access resource I have to pass two ids, because two ids build the key.

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

I get nulls in controller action with following signature:

public async Task<IActionResult> Details(int? id, int? customerId)

I use default routing. What should I do to pass both ids?

Upvotes: 0

Views: 2887

Answers (2)

Camilo Terevinto
Camilo Terevinto

Reputation: 32068

There are multiple ways to get this done. One of the ways is to build a ViewModel to contain both Ids. Another way is to use the HttpGetAttribute to specify the route:

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

Here, {id} will map to id and {customerId} to customerId, provided that the received values are int

Upvotes: 1

T.Aoukar
T.Aoukar

Reputation: 663

You'll have to define the source for second ID, can be from header, query param, or route param. Default routing defines the route as /{controller}/{action}/{?id} as you can see, it tells MVC where to get the id from the route.

In your case, I suggest sending both IDs as query params.

This link could be helpful for you on how to pass argument as query params.

Upvotes: 0

Related Questions