'How to call a controller Action passing id parameters from a modal partialView?

I have an ASP.Net Core MVC5 project with a view containing partialViews. In it I show a Bootstrap modal that loads data from a record in context. I need the identifier of this record to be passed as a parameter in an <a> tag that redirects to another action of the same Controller.

I tried to mount it like this:

<a asp-controller="Controller" asp-action="Action" asp-route="@ViewBag.Value" class="btn btn-default">
  <i class="fas fa-file-invoice-dollar"></i> 
  Button
</a>

However it doesn't work, it doesn't recognize the ViewBag.

I also tried with Onclick script, like this, but the id parameter arrives as null.

$('#idA').attr('action', '/Controller/Action/'@ViewBag.id);

Has anyone done something like this or can help me?



Solution 1:[1]

You can use asp-route-id

<a asp-controller="Controller" asp-action="Action" asp-route="@ViewBag.Value" asp-route-id="@ViewBag.id" class="btn btn-default">
  <i class="fas fa-file-invoice-dollar"></i> 
  Button
</a>

Solution 2:[2]

You can use javascript or jquery for getting the values and ajax to post to the controller, something like this:

https://stackoverflow.com/a/10653696/7687161

Solution 3:[3]

You should not combine asp-route="xx" and asp-controller="xx" try with this:

<a asp-controller="Controller" asp-action="Action" asp-route-Id="@ViewBag.Id" class="btn btn-default">
    <i class="fas fa-file-invoice-dollar"></i>
    Button
</a>

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Asger Vestbjerg
Solution 2 Rafael Pérez
Solution 3 Ruikai Feng