0

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?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Just to make sure whether id is available in modal, are you able to display (like a label or some element) idA with viewBag.id on the modal dialog ? – sam May 20 '22 at 13:38
  • The idA is already displayed in the modal currently with a span – Ivan Peixoto May 20 '22 at 13:55

3 Answers3

0

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>
Asger Vestbjerg
  • 217
  • 1
  • 3
  • 10
0

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

0

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

Ruikai Feng
  • 6,823
  • 1
  • 2
  • 11